//import java packages
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.*;
import java.io.*;
import javax.swing.undo.*;
import javax.swing.border.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.event.*;
import java.awt.datatransfer.*;
class Notepad extends JFrame implements ActionListener,DocumentListener{
//定义变量
//菜单
JMenu fileMenu,editMenu,formatMenu,viewMenu,helpMenu;
//"文件"的菜单项
JMenuItem fileMenu_New,fileMenu_Open,fileMenu_Save,fileMenu_SaveAs,fileMenu_PageSetup,fileMenu_Print,fileMenu_Exit;
//"编辑"的菜单项
JMenuItem editMenu_Undo,editMenu_Redo,editMenu_Cut,editMenu_Copy,editMenu_Paste,editMenu_Delete,editMenu_Find,editMenu_FindNext,editMenu_Replace,
editMenu_GoTo,editMenu_SelectAll,editMenu_TimeDate;
//"格式"的菜单项
JCheckBoxMenuItem formatMenu_LineWrap;
JMenu formatMenu_Color;
JMenuItem formatMenu_Font,formatMenu_Color_FgColor,formatMenu_Color_BgColor;
//"查看"的菜单项
JCheckBoxMenuItem viewMenu_Status;
//"帮助"的菜单项
JMenuItem helpMenu_HelpTopics,helpMenu_About;
//文本编辑区域
JTextArea editArea;
//状态栏标签
JLabel statusLabel;
//弹出菜单及菜单项
JPopupMenu popupMenu;
JMenuItem popupMenu_Undo,popupMenu_Redo,popupMenu_Cut,popupMenu_Copy,popupMenu_Paste,popupMenu_Delete,popupMenu_SelectAll;
//系统剪贴板
Toolkit toolKit=Toolkit.getDefaultToolkit();
Clipboard clipBoard=toolKit.getSystemClipboard();
//其它变量
boolean isNewFile=true; //是否新文件(未保存过的)
File currentFile; //当前文件名
String oldValue; //存放编辑区原来的内容,用于比较文本是否有改动
JButton fontOkButton; //字体设置里的"确定"按钮
//工具栏按钮
JButton newButton,openButton,saveButton,saveAsButton,printButton,undoButton,redoButton,
cutButton,copyButton,pasteButton,deleteButton,searchButton,timeButton,fontButton,
boldButton,italicButton,fgcolorButton,bgcolorButton,helpButton;
//创建撤消操作管理器
protected UndoManager undo = new UndoManager();
protected UndoableEditListener undoHandler = new UndoHandler();
//设置编辑区默认字体
protected Font defaultFont=new Font("宋体",Font.PLAIN,12);
//构造函数开始
public Notepad()
{
super("黄斌的记事本");
//获取容器
Container container=getContentPane();
//创建菜单条
JMenuBar menuBar = new JMenuBar();
//创建文件菜单及菜单项并注册事件监听
JMenu fileMenu = new JMenu("文件(F)", true);
fileMenu.setMnemonic('F');
fileMenu_New = new JMenuItem("新建(N)", 'N');
fileMenu_New.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_MASK));
fileMenu_New.addActionListener(this);
fileMenu_Open = new JMenuItem("打开(O)...", 'O');
fileMenu_Open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_MASK));
fileMenu_Open.addActionListener(this);
fileMenu_Save = new JMenuItem("保存(S)", 'S');
fileMenu_Save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK));
fileMenu_Save.addActionListener(this);
fileMenu_SaveAs = new JMenuItem("另存为(A)...", 'A');
fileMenu_SaveAs.addActionListener(this);
fileMenu_PageSetup = new JMenuItem("页面设置(U)...",'U');
fileMenu_PageSetup.addActionListener(this);
fileMenu_Print = new JMenuItem("打印(P)...", 'P');
fileMenu_Print.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P, InputEvent.CTRL_MASK));
fileMenu_Print.addActionListener(this);
fileMenu_Exit = new JMenuItem("退出(X)",'X');
fileMenu_Exit.addActionListener(this);
//创建编辑菜单及菜单项并注册事件监听
JMenu editMenu = new JMenu("编辑(E)", true);
editMenu.setMnemonic('E');
editMenu.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e){
checkMenuItemEnabled();//设置剪切、复制、粘贴、删除等功能的可用性
}
}
);
editMenu_Undo = new JMenuItem("撤消(U)",'U');
editMenu_Undo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z, InputEvent.CTRL_MASK));
editMenu_Undo.addActionListener(this);
editMenu_Undo.setEnabled(false);
editMenu_Redo = new JMenuItem("重做(R)",'R');
editMenu_Redo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Y,InputEvent.CTRL_MASK));
editMenu_Redo.addActionListener(this);
editMenu_Redo.setEnabled(false);
editMenu_Cut = new JMenuItem("剪切(T)",'T');
editMenu_Cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_MASK));
editMenu_Cut.addActionListener(this);
editMenu_Copy = new JMenuItem("复制(C)",'C');
editMenu_Copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK));
editMenu_Copy.addActionListener(this);
editMenu_Paste = new JMenuItem("粘贴(P)",'P');
editMenu_Paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_MASK));
editMenu_Paste.addActionListener(this);
editMenu_Delete = new JMenuItem("删除(L)",'L');
editMenu_Delete.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0));
editMenu_Delete.addActionListener(this);
editMenu_Find = new JMenuItem("查找(F)...",'F');
editMenu_Find.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, InputEvent.CTRL_MASK));
editMenu_Find.addActionListener(this);
editMenu_FindNext = new JMenuItem("查找下一个(N)",'N');
editMenu_FindNext.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F3,0));
editMenu_FindNext.addActionListener(this);
editMenu_Replace = new JMenuItem("替换(R)...",'R');
editMenu_Replace.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H, InputEvent.CTRL_MASK));
editMenu_Replace.addActionListener(this);
editMenu_GoTo = new JMenuItem("转到(G)...",'G');
editMenu_GoTo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_G, InputEvent.CTRL_MASK));
editMenu_GoTo.addActionListener(this);
editMenu_SelectAll = new JMenuItem("全选",'A');
editMenu_SelectAll.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, InputEvent.CTRL_MASK));
editMenu_SelectAll.addActionListener(this);
editMenu_TimeDate = new JMenuItem("时间/日期(D)",'D');
editMenu_TimeDate.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F5,0));
editMenu_TimeDate.addActionListener(this);
//创建格式菜单及菜单项并注册事件监听
JMenu formatMenu = new JMenu("格式(O)", true);
formatMenu.setMnemonic('O');
formatMenu_LineWrap = new JCheckBoxMenuItem("自动换行(W)");
formatMenu_LineWrap.setMnemonic('W');
formatMenu_LineWrap.setState(true);
formatMenu_LineWrap.addActionListener(this);
formatMenu_Font = new JMenuItem("字体(F)...",'F');
formatMenu_Font.addActionListener(this);
formatMenu_Color = new JMenu("设置颜色");
formatMenu_Color_FgColor=new JMenuItem("字体颜色");
formatMenu_Color_FgColor.addActionListener(this);
formatMenu_Color_BgColor=new JMenuItem("背景颜色");
formatMenu_Color_BgColor.addActionListener(this);
//创建查看菜单及菜单项并注册事件监听
JMenu viewMenu = new JMenu("查看(V)", true);
viewMenu.setMnemonic('V');
viewMenu_Status = new JCheckBoxMenuItem("状态栏(S)");
viewMenu_Status.setMnemonic('S');
viewMenu_Status.setState(true);
viewMenu_Status.addActionListener(this);
//创建帮助菜单及菜单项并注册事件监听
JMenu helpMenu = new JMenu("帮助(H)", true);
helpMenu.setMnemonic('H');
helpMenu_HelpTopics = new JMenuItem("帮助主题(H)",'H');
helpMenu_HelpTopics.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1,0));
helpMenu_HelpTopics.addActionListener(this);
helpMenu_About = new JMenuItem("关于记事本(A)",'A');
helpMenu_About.addActionListener(this);
//向菜单条添加"文件"菜单及菜单项
menuBar.add(fileMenu);
fileMenu.add(fileMenu_New);
fileMenu.add(fileMenu_Open);
fileMenu.add(fileMenu_Save);
fileMenu.add(fileMenu_SaveAs);
fileMenu.addSeparator(); //分隔线
fileMenu.add(fileMenu_PageSetup);
fileMenu.add(fileMenu_Print);
fileMenu.addSeparator(); //分隔线
fileMenu.add(fileMen