logo资料库

Eclipse开发记事本程序.doc

第1页 / 共3页
第2页 / 共3页
第3页 / 共3页
资料共3页,全文预览结束
Eclipse 开发记事本程序 1、新建项目,新建 Visual Class,名称输入 AppText,Style 选 Swing 下的 Frame,选中 public static void main(String[] args),完成。 2、进入 AppText 中心面板,右击鼠标,设置中心面板的 Layout 为 null 布 局。 3、在 palette 的 Swing Containers 下选择 jScrollPane,建立滚动面板, 选择 JjTextArea 建立多行文本,重命名为 jTextArea。 4、建立菜单栏 jJMenuBar,插入四个菜单 JMenu,分别改名为文件、编辑、 工具、帮助;在“文件”下插入菜单项新建、打开、保存、退出;在“编辑”下 插入菜单项复制、剪切、粘贴、全选、颜色;在“工具”下插入菜单项计算器、 记事本;在“帮助”插入菜单项关于。 5、给“新建”菜单添加 actionPerformed 事件,在对应的代码处写入: jTextArea.setText(null); AppText.this.setTitle("无标题 - 记事本"); 6、给“打开”菜单添加 actionPerformed 事件,在对应的代码处写入: File f1; JFileChooser jfc1 = new JFileChooser(); int num1 = jfc1.showOpenDialog(AppText.this); if(num1==JFileChooser.APPROVE_OPTION){ try{ f1 = jfc1.getSelectedFile(); AppText.this.setTitle(f1.getName()); FileReader fr = new FileReader(f1); BufferedReader br = new BufferedReader(fr); String str; while((str = br.readLine())!=null){ jTextArea.setText(str); } fr.close(); br.close(); }catch(FileNotFoundException e1){ e1.printStackTrace(); }catch(IOException e2){ e2.printStackTrace(); } } 7、给“保存”菜单添加 actionPerformed 事件,在对应的代码处写入:
File f2 = null; JFileChooser jfc2 = new JFileChooser(); int num2 = jfc2.showSaveDialog(AppText.this); if(num2==JFileChooser.APPROVE_OPTION){ f2=jfc2.getSelectedFile(); AppText.this.setTitle(f2.getName()); try{ FileWriter fw = new FileWriter(f2); BufferedWriter bw = new BufferedWriter(fw); bw.write(jTextArea.getText()); bw.close(); fw.close(); }catch(IOException e2){ e2.printStackTrace(); } } 8、给“退出”菜单添加 actionPerformed 事件,在对应的代码处写入: int a = JOptionPane.showConfirmDialog(AppText.this,"文件已被改 变,是否要保存?","提示",JOptionPane.YES_NO_CANCEL_OPTION); if(a==1){ AppText.this.dispose(); }else if(a==0){ File f2 = null; JFileChooser jfc2 = new JFileChooser(); int num2 = jfc2.showSaveDialog(AppText.this); if(num2==JFileChooser.APPROVE_OPTION){ f2=jfc2.getSelectedFile(); AppText.this.setTitle(f2.getName()); try{ FileWriter fw = new FileWriter(f2); BufferedWriter bw = new BufferedWriter(fw); bw.write(jTextArea.getText()); bw.close(); fw.close(); }catch(IOException e2){ e2.printStackTrace(); } AppText.this.dispose(); } } 9、给“编辑”菜单中的复制、剪切、粘贴、全选添加 actionPerformed 事
件,在对应的代码处分别写入: jTextArea.copy(); jTextArea.cut(); jTextArea.paste(); jTextArea.selectAll(); 10、给“颜色”菜单添加 actionPerformed 事件,在对应的代码处写入: Color color=JColorChooser.showDialog(jTextArea, "选择颜色", Color.BLACK); jTextArea.setForeground(color); 11、给“记事本”菜单添加 actionPerformed 事件,在对应的代码处写入: try{ } String command = "notepad.exe"; Process child = Runtime.getRuntime().exec(command); catch (IOException ex) { } 12、给“计算器”菜单添加 actionPerformed 事件,在对应的代码处写入: try{ } String command = "calc.exe"; Process child = Runtime.getRuntime().exec(command); catch (IOException ex) { } 13、给“关于”菜单添加 actionPerformed 事件,在对应的代码处写入: String message = "--------\n版本:1.0\n作者:曹勇" + "\n\n感谢您的使用!"; JOptionPane.showMessageDialog(AppText.this, message, "关于", JOptionPane.PLAIN_MESSAGE); 注意:不能识别的文件需导入包,在错误处单击,选择需导入的包文件,双击引入。 窗口剧中代码 this.setLocationRelativeTo(null);
分享到:
收藏