BufferedReader(new FileReader(selectedFile));
try {
BufferedReader
bf=new
String tempString=\ inti; while((i=bf.read())!=-1) { tempString+=String.valueOf((char)i); } jta.setText(tempString); bf.close();
} catch (Exception e) { // TODO: handle exception
JOptionPane.showConfirmDialog(Notepad.this, \文件保存(S) Ctrl+S错误\
} } else {} } } else {} } else { JFileChooserjChooser=new JFileChooser(); int selection=jChooser.showOpenDialog(Notepad.this); if(selection==JFileChooser.APPROVE_OPTION) { selectedFile=jChooser.getSelectedFile(); try { BufferedReader bf=new BufferedReader(new FileReader(selectedFile)); String tempString=\ inti; while((i=bf.read())!=-1) { tempString+=String.valueOf((char)i); } jta.setText(tempString); bf.close(); } catch (Exception e) { // TODO: handle exception JOptionPane.showConfirmDialog(Notepad.this, \文
件打开(O) Ctrl+O错误\ } } });
}
}
else {}
JMenuItemjmiSaveItem=new JMenuItem(\保存(S) Ctrl+S\
jmiSaveItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub if(jta.getText().equals(\ {} else { if(selectedFile==null) { JFileChooserjChooser=new JFileChooser(); int selection=jChooser.showOpenDialog(Notepad.this); if(selection==JFileChooser.APPROVE_OPTION) { selectedFile=jChooser.getSelectedFile(); try { BufferedReader bf=new BufferedReader(new FileReader(selectedFile)); String tempString=\ inti; while((i=bf.read())!=-1) { tempString+=String.valueOf((char)i); } jta.setText(tempString); bf.close(); } catch (Exception e) { // TODO: handle exception JOptionPane.showConfirmDialog(Notepad.this, \文件打开(O) Ctrl+O错误\ } } else {} } else
常\
{ }
try { FileWriterfw=new FileWriter(selectedFile); fw.write(jta.getText()); fw.close();
} catch (Exception e) { // TODO: handle exception JOptionPane.showConfirmDialog(Notepad.this, \文件写入异}
} }});
JMenuItemjmiSaveTo = new JMenuItem(\另存为(A)...\ jmiSaveTo.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub JFileChooserjChooser=new JFileChooser(); int selection=jChooser.showOpenDialog(Notepad.this); if(selection==JFileChooser.APPROVE_OPTION) { selectedFile=jChooser.getSelectedFile(); try { BufferedWriter bf=new BufferedWriter(new FileWriter(selectedFile)); bf.write(jta.getText()); bf.close(); } catch (Exception e) { // TODO: handle exception JOptionPane.showConfirmDialog(Notepad.this, \文件打开(O) Ctrl+O错误\ } } else {} } }); JMenuItemjmiExitItem=new JMenuItem(\退出(X)\ jmiExitItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub if(jta.getText().equals(\
System.exit(0); else { int t=JOptionPane.showConfirmDialog(Notepad.this, \确定退出(X)?\ if(t==JOptionPane.OK_OPTION) { System.exit(0); } } } });
JMenuItemjmiPrintItem = new JMenuItem(\打印(P) Ctrl+P\jmiPrintItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE; PrintRequestAttributeSetaset = new HashPrintRequestAttributeSet(); aset.add(MediaSizeName.ISO_A4); PrintService[] pservices = PrintServiceLookup.lookupPrintServices(flavor, aset); PrintServicedefaultService=PrintServiceLookup.lookupDefaultPrintService(); if (pservices.length> 0) { PrintService service = ServiceUI.printDialog(null, 200, 200, pservices, defaultService, flavor, aset); DocPrintJobpj = service.createPrintJob(); try { String fis=jta.getText(); DocAttributeSet das = new HashDocAttributeSet(); Doc doc = new SimpleDoc(fis, flavor, das); pj.print(doc, aset); } catch (PrintException e) { e.printStackTrace(); } } } });
jmFile.add(jmiNewItem); jmFile.add(jmiOpenItem); jmFile.add(jmiSaveItem); jmFile.add(jmiSaveTo);
jmFile.add(new JSeparator());
jmFile.add(jmiPrintItem); jmFile.add(new JSeparator()); jmFile.add(jmiExitItem);
//******************************实现编辑菜单的相关item
//***************************由于item太多,就放在一个类中实现监听(除了jmiUndoItem) jmiUndoItem = new JMenuItem(\撤销(U) Ctrl+Z\ jmiUndoItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub if(canUndo) { jmiUndoItem.setEnabled(true); edit.undo(); } else { jmiUndoItem.setEnabled(false); } } });
JMenuItemjmiCutItem = new JMenuItem(\剪切(T) Ctrl+T\jmiCutItem.addActionListener(new EditListener());
JMenuItemjmiCopyItem = new JMenuItem(\复制(C) Ctrl+C\jmiCopyItem.addActionListener(new EditListener());
JMenuItemjmiPasteItem = new JMenuItem(\粘贴(P) Ctrl+V\jmiPasteItem.addActionListener(new EditListener());
JMenuItemjmiDeleteItem = new JMenuItem(\删除(L) Del\jmiDeleteItem.addActionListener(new EditListener());
JMenuItemjmiSearchItem = new JMenuItem(\查找(F) Ctrl+F\jmiSearchItem.addActionListener(new EditListener());
JMenuItemjmiSearchNextItem = new JMenuItem(\查找下一个(N) F3\jmiSearchNextItem.addActionListener(new EditListener()); JMenuItemjmiReplaceItem = new JMenuItem(\替换\jmiReplaceItem.addActionListener(new EditListener()); JMenuItemjmiGoToItem = new JMenuItem(\转到\jmiGoToItem.addActionListener(new EditListener()); JMenuItemjmiSelectAllItem = new JMenuItem(\全选\jmiSelectAllItem.addActionListener(new EditListener()); JMenuItemjmiDateItem = new JMenuItem(\日期\jmiDateItem.addActionListener(new EditListener());