主程序:
importjava.awt.*;
importjava.awt.datatransfer.*; importjava.awt.event.*;
importjavax.print.*;
importjavax.print.attribute.DocAttributeSet;
importjavax.print.attribute.HashDocAttributeSet;
importjavax.print.attribute.HashPrintRequestAttributeSet; importjavax.print.attribute.PrintRequestAttributeSet; importjavax.print.attribute.standard.MediaSizeName; importjavax.swing.*;
importjavax.swing.border.BevelBorder;
importjavax.swing.event.UndoableEditEvent; importjavax.swing.event.UndoableEditListener; importjavax.swing.filechooser.*;
importjavax.swing.filechooser.FileFilter; importjavax.swing.plaf.FileChooserUI; importjavax.swing.undo.UndoableEdit;
import java.io.*;
public class Notepad extends JFrame { /** * @paramargs */
public JTextAreajta=null; //中间的文本区域 private File selectedFile=null;
private JFileChooser chooser=null; //文件选择器 private UndoableEdit edit; //设置还原功能 privatebooleancanUndo=false;
private JMenuItemjmiUndoItem; //撤销的菜单 private Clipboard clip=null; //粘贴板 privateJDialogjdSearchDialog;
public Notepad() { super(\ super.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); chooser=new JFileChooser(); chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); clip=Toolkit.getDefaultToolkit().getSystemClipboard(); jta=new JTextArea();
jta.setWrapStyleWord(true); jta.setLineWrap(true);
//*****************************右键弹出功能 finalJPopupMenujpMenu=new JPopupMenu();
JMenuItemjpItemUndo=new JMenuItem(\撤销(U) \ JMenuItemjpItemCut=new JMenuItem(\剪切(T) \ JMenuItemjpItemCopy=new JMenuItem(\复制(C) \ JMenuItemjpItemPaste=new JMenuItem(\粘贴(P) \ jpMenu.add(jpItemUndo); jpMenu.add(new JPopupMenu.Separator()); jpMenu.add(jpItemCut); jpMenu.add(jpItemCopy); jpMenu.add(jpItemPaste); jpMenu.setBorder(new BevelBorder(BevelBorder.RAISED)); jta.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { if(e.isMetaDown()) { if(!jpMenu.isVisible()) { jpMenu.show(e.getComponent(),e.getX(),e.getY()); } } else { jpMenu.setVisible(false); } } });
//*****************************实现撤销功能 jta.getDocument().addUndoableEditListener(new UndoableEditListener() { @Override public void undoableEditHappened(UndoableEditEvent e) { // TODO Auto-generated method stub edit=e.getEdit(); if(edit!=null) { canUndo=edit.canUndo(); } else
{ canUndo=false; } } });
//**********************************设置相关菜单和事件监听 Container content=getContentPane(); JMenuBarjmb=new JMenuBar();
JMenujmFile=new JMenu(\文件(F)\jmFile.setMnemonic('f');
JMenujmEdit=new JMenu(\编辑(E)\jmEdit.setMnemonic('e');
JMenujmStyle=new JMenu(\格式(O)\jmStyle.setMnemonic('e');
JMenujmView=new JMenu(\查看(V)\jmView.setMnemonic('v');
JMenujmHelp=new JMenu(\帮助(H)\ jmHelp.setMnemonic('h'); jmb.add(jmFile); jmb.add(jmEdit); jmb.add(jmStyle); jmb.add(jmView); jmb.add(jmHelp); setJMenuBar(jmb); JMenuItemjmiNewItem=new JMenuItem(\新建(N) Ctrl+N\ jmiNewItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub if(!jta.getText().equals(\ { int confirm=JOptionPane.showConfirmDialog(Notepad.this, \是否保存(S) Ctrl+S文件?\ if(confirm==JOptionPane.OK_OPTION) { if(selectedFile!=null) { try { FileWriterfw=new FileWriter(selectedFile); fw.write(jta.getText()); fw.close();
入异常\
} catch (Exception e) { // TODO: handle exception JOptionPane.showConfirmDialog(Notepad.this, \文件写
} jta.setText(\} else { jta.setText(\ JFileChooserjChooser=new JFileChooser(); int selection=jChooser.showSaveDialog(Notepad.this); if(selection==JFileChooser.APPROVE_OPTION) { selectedFile=jChooser.getSelectedFile(); try { FileWriterfw=new FileWriter(selectedFile); fw.write(jta.getText()); fw.close(); } catch (Exception e) { // TODO: handle exception
JOptionPane.showConfirmDialog(Notepad.this, \文件写入异常\
} } } } else if(confirm==JOptionPane.NO_OPTION) { jta.setText(\ } else {} } else{} } }); JMenuItemjmiOpenItem=new JMenuItem(\打开(O) Ctrl+O\ jmiOpenItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub if(!jta.getText().equals(\ {
Ctrl+S文件?\ 入异常\
int confirm=JOptionPane.showConfirmDialog(Notepad.this, \是否保存(S) if(confirm==JOptionPane.OK_OPTION) { if(selectedFile!=null) { try { FileWriterfw=new FileWriter(selectedFile); fw.write(jta.getText()); fw.close(); } catch (Exception e) { // TODO: handle exception
JOptionPane.showConfirmDialog(Notepad.this, \文件写
} } else { JFileChooserjChooser=new JFileChooser(); int selection=jChooser.showSaveDialog(Notepad.this); if(selection==JFileChooser.APPROVE_OPTION) { selectedFile=jChooser.getSelectedFile(); try { FileWriterfw=new FileWriter(selectedFile); fw.write(jta.getText()); fw.close(); } catch (Exception e) { // TODO: handle exception
JOptionPane.showConfirmDialog(Notepad.this, \文件写入异常\
} } } } else if(confirm==JOptionPane.NO_OPTION) { jta.setText(\ JFileChooserjChooser=new JFileChooser(); int selection=jChooser.showOpenDialog(Notepad.this); { if(selection==JFileChooser.APPROVE_OPTION) { selectedFile=jChooser.getSelectedFile();