2021-04-09

package myproject;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.*;public class Example1 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 Example1(){super(&"Windows记事本&");//获取容器Container container=getContentPane();//创建菜单条JMenuBar menuBar = new JMenuBar();//创建文件菜单及菜单项并注册事件监听JMenu fileMenu = new JMenu(&"文件(F)&", true);fileMenu.setMnemonic(F); //设置快捷键ALT+FfileMenu_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);//设置快捷键ALT+E//当选择编辑菜单时,设置剪切、复制、粘贴、删除等功能的可用性 editMenu.addMenuListener(new MenuListener() { public void menuCanceled(MenuEvent e)//取消菜单时调用 { checkMenuItemEnabled();//设置剪切、复制、粘贴、删除等功能的可用性 } public void menuDeselected(MenuEvent e)//取消选择某个菜单时调用 { checkMenuItemEnabled();//设置剪切、复制、粘贴、删除等功能的可用性 } public void menuSelected(MenuEvent 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_R, 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);//设置快捷键ALT+OformatMenu_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);//设置快捷键ALT+VviewMenu_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);//设置快捷键ALT+HhelpMenu_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(fileMenu_Exit);//向菜单条添加&"编辑&"菜单及菜单项menuBar.add(editMenu);editMenu.add(editMenu_Undo);editMenu.add(editMenu_Redo);editMenu.addSeparator(); //分隔线editMenu.add(editMenu_Cut);editMenu.add(editMenu_Copy);editMenu.add(editMenu_Paste);editMenu.add(editMenu_Delete);editMenu.addSeparator(); //分隔线editMenu.add(editMenu_Find);editMenu.add(editMenu_FindNext);editMenu.add(editMenu_Replace);editMenu.add(editMenu_GoTo);editMenu.addSeparator(); //分隔线editMenu.add(editMenu_SelectAll);editMenu.add(editMenu_TimeDate);//向菜单条添加&"格式&"菜单及菜单项 menuBar.add(formatMenu);formatMenu.add(formatMenu_LineWrap);formatMenu.add(formatMenu_Font);formatMenu.addSeparator();//分隔线formatMenu.add(formatMenu_Color);formatMenu_Color.add(formatMenu_Color_FgColor);formatMenu_Color.add(formatMenu_Color_BgColor);//向菜单条添加&"查看&"菜单及菜单项menuBar.add(viewMenu);viewMenu.add(viewMenu_Status);//向菜单条添加&"帮助&"菜单及菜单项menuBar.add(helpMenu);helpMenu.add(helpMenu_HelpTopics);helpMenu.addSeparator();//分隔线helpMenu.add(helpMenu_About);//向窗口添加菜单条 this.setJMenuBar(menuBar);//创建文本编辑区并添加滚动条editArea=new JTextArea(20,50);JScrollPane scroller=new JScrollPane(editArea);//分别设置水平和垂直滚动条总是出现 scroller.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); scroller.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); container.add(scroller,BorderLayout.CENTER);//向容器添加文本编辑区editArea.setWra代做Java实验、Java作业代做留学生、代做Java程序、Java作业调试、帮做Java编程作业pStyleWord(true); //设置单词在一行不足容纳时换行editArea.setLineWrap(true); //设置文本编辑区自动换行默认为true,即会&"自动换行&"editArea.setFont(defaultFont); //设置编辑区默认字体editArea.setBackground(Color.white); //设置编辑区默认背景色editArea.setForeground(Color.black); //设置编辑区默认前景色ldValue=editArea.getText(); //获取原文本编辑区的内容 //编辑区注册事件监听editArea.getDocument().addUndoableEditListener(undoHandler);editArea.getDocument().addDocumentListener(this);//创建右键弹出菜单popupMenu=new JPopupMenu();popupMenu_Undo=new JMenuItem(&"撤消(U)&",U);popupMenu_Redo=new JMenuItem(&"重做(R)&",R);popupMenu_Cut =new JMenuItem(&"剪切(T)&",T);popupMenu_Copy=new JMenuItem(&"复制(C)&",C);popupMenu_Paste=new JMenuItem(&"粘贴(P)&",P);popupMenu_Delete=new JMenuItem(&"删除(D)&",D);popupMenu_SelectAll=new JMenuItem(&"全选(A)&",A); popupMenu_Undo.setEnabled(false); //撤消选项初始设为不可用popupMenu_Redo.setEnabled(false); //重做选项初始设为不可用//向右键菜单添加菜单项和分隔符popupMenu.add(popupMenu_Undo);popupMenu.add(popupMenu_Redo);popupMenu.addSeparator();popupMenu.add(popupMenu_Cut);popupMenu.add(popupMenu_Copy); popupMenu.add(popupMenu_Paste); popupMenu.add(popupMenu_Delete);popupMenu.addSeparator();popupMenu.add(popupMenu_SelectAll);//右键菜单注册事件popupMenu_Undo.addActionListener(this);popupMenu_Redo.addActionListener(this);popupMenu_Cut.addActionListener(this);popupMenu_Copy.addActionListener(this);popupMenu_Paste.addActionListener(this);popupMenu_Delete.addActionListener(this);popupMenu_SelectAll.addActionListener(this);//文本编辑区注册右键菜单事件editArea.addMouseListener(new MouseAdapter(){public void mousePressed(MouseEvent e){checkForTriggerEvent(e);}public void mouseReleased(MouseEvent e){checkForTriggerEvent(e);}private void checkForTriggerEvent(MouseEvent e){if(e.isPopupTrigger())//返回此鼠标事件是否为该平台的弹出菜单触发事件 popupMenu.show(e.getComponent(),e.getX(),e.getY());//在组件调用者的坐标空间中的位置 X、Y 显示弹出菜单 checkMenuItemEnabled(); //设置剪切、复制、粘贴、删除等功能的可用性editArea.requestFocus(); //编辑区获取焦点} });//文本编辑区注册右键菜单事件结束//创建工具栏JPanel toolBar=new JPanel();toolBar.setLayout(new FlowLayout(FlowLayout.LEFT));Icon newIcon=new ImageIcon(&"Icons/new.gif&");Icon penIcon=new ImageIcon(&"Icons/open.gif&");Icon saveIcon=new ImageIcon(&"Icons/save.gif&");Icon saveAsIcon=new ImageIcon(&"Icons/saveas.gif&");Icon printIcon=new ImageIcon(&"Icons/print.gif&");Icon undoIcon=new ImageIcon(&"Icons/undo.gif&");Icon redoIcon=new ImageIcon(&"Icons/redo.gif&");Icon cutIcon=new ImageIcon(&"Icons/cut.gif&");Icon copyIcon=new ImageIcon(&"Icons/copy.gif&");Icon pasteIcon=new ImageIcon(&"Icons/paste.gif&");Icon deleteIcon=new ImageIcon(&"Icons/delete.gif&"); Icon searchIcon=new ImageIcon(&"Icons/search.gif&");Icon timeIcon=new ImageIcon(&"Icons/time.gif&");Icon fontIcon=new ImageIcon(&"Icons/font.gif&");Icon boldIcon=new ImageIcon(&"Icons/bold.gif&");Icon italicIcon=new ImageIcon(&"Icons/italic.gif&");Icon bgcolorIcon=new ImageIcon(&"Icons/bgcolor.gif&");Icon fgcolorIcon=new ImageIcon(&"Icons/fgcolor.gif&");Icon helpIcon=new ImageIcon(&"Icons/help.gif&");newButton= new JButton(newIcon);penButton=new JButton(openIcon);saveButton= new JButton(saveIcon);saveAsButton=new JButton(saveAsIcon);printButton=new JButton(printIcon);undoButton=new JButton(undoIcon);undoButton.setEnabled(false);redoButton=new JButton(redoIcon);redoButton.setEnabled(false);cutButton=new JButton(cutIcon);cutButton.setEnabled(false);copyButton=new JButton(copyIcon);copyButton.setEnabled(false);pasteButton=new JButton(pasteIcon);pasteButton.setEnabled(false);deleteButton=new JButton(deleteIcon);deleteButton.setEnabled(false);searchButton=new JButton(searchIcon);timeButton=new JButton(timeIcon);fontButton=new JButton(fontIcon);boldButton=new JButton(boldIcon);italicButton=new JButton(italicIcon);fgcolorButton=new JButton(fgcolorIcon);bgcolorButton=new JButton(bgcolorIcon); helpButton=new JButton(helpIcon);newButton.setPreferredSize(new Dimension(22,22));openButton.setPreferredSize(new Dimension(22,22));saveButton.setPreferredSize(new Dimension(22,22));saveAsButton.setPreferredSize(new Dimension(22,22));printButton.setPreferredSize(new Dimension(22,22));undoButton.setPreferredSize(new Dimension(22,22));redoButton.setPreferredSize(new Dimension(22,22));cutButton.setPreferredSize(new Dimension(22,22));copyButton.setPreferredSize(new Dimension(22,22));pasteButton.setPreferredSize(new Dimension(22,22));deleteButton.setPreferredSize(new Dimension(22,22));searchButton.setPreferredSize(new Dimension(22,22));timeButton.setPreferredSize(new Dimension(22,22));fontButton.setPreferredSize(new Dimension(22,22));boldButton.setPreferredSize(new Dimension(22,22));italicButton.setPreferredSize(new Dimension(22,22));fgcolorButton.setPreferredSize(new Dimension(22,22));bgcolorButton.setPreferredSize(new Dimension(22,22)); helpButton.setPreferredSize(new Dimension(22,22));//注册工具栏按钮事件newButton.addActionListener(this);openButton.addActionListener(this);saveButton.addActionListener(this);saveAsButton.addActionListener(this);printButton.addActionListener(this);undoButton.addActionListener(this);redoButton.addActionListener(this);cutButton.addActionListener(this);copyButton.addActionListener(this);pasteButton.addActionListener(this);deleteButton.addActionListener(this);searchButton.addActionListener(this);timeButton.addActionListener(this);fontButton.addActionListener(this);boldButton.addActionListener(this);italicButton.addActionListener(this);fgcolorButton.addActionListener(this);bgcolorButton.addActionListener(this); helpButton.addActionListener(this);//设置按钮提示文字newButton.setToolTipText(&"新建&");openButton.setToolTipText(&"打开&");saveButton.setToolTipText(&"保存&");saveAsButton.setToolTipText(&"另存为&");printButton.setToolTipText(&"打印&");undoButton.setToolTipText(&"撤消&");redoButton.setToolTipText(&"重做&");cutButton.setToolTipText(&"剪切&");copyButton.setToolTipText(&"复制&");pasteButton.setToolTipText(&"粘贴&");deleteButton.setToolTipText(&"删除所选&");searchButton.setToolTipText(&"查找与替换&");timeButton.setToolTipText(&"插入时间/日期&");fontButton.setToolTipText(&"设置字体&");boldButton.setToolTipText(&"粗体&");italicButton.setToolTipText(&"斜体&");fgcolorButton.setToolTipText(&"设置字体颜色&");bgcolorButton.setToolTipText(&"设置背景颜色&"); helpButton.setToolTipText(&"帮助&");//设置撤消、重做、剪切、复制、粘贴、删除等工具栏按钮不可用时的图片(灰色)undoButton.setDisabledIcon(new ImageIcon(&"Icons/undo1.gif&"));redoButton.setDisabledIcon(new ImageIcon(&"Icons/redo1.gif&"));cutButton.setDisabledIcon(new ImageIcon(&"Icons/cut1.gif&"));copyButton.setDisabledIcon(new ImageIcon(&"Icons/copy1.gif&"));pasteButton.setDisabledIcon(new ImageIcon(&"Icons/paste1.gif&"));deleteButton.setDisabledIcon(new ImageIcon(&"Icons/delete1.gif&"));//向工具栏添加按钮toolBar.add(newButton);toolBar.add(openButton);toolBar.add(saveButton);toolBar.add(saveAsButton);toolBar.add(printButton);toolBar.add(undoButton);toolBar.add(redoButton);toolBar.add(cutButton);toolBar.add(copyButton);toolBar.add(pasteButton);toolBar.add(deleteButton);toolBar.add(searchButton);toolBar.add(timeButton);toolBar.add(fontButton);toolBar.add(boldButton);toolBar.add(italicButton);toolBar.add(fgcolorButton);toolBar.add(bgcolorButton);toolBar.add(helpButton);//向容器添加工具栏container.add(toolBar,BorderLayout.NORTH);//创建和添加状态栏statusLabel=new JLabel(&" 按F1获取帮助&");container.add(statusLabel,BorderLayout.SOUTH);//改变标题栏窗口左侧默认图标 Toolkit tk=Toolkit.getDefaultToolkit(); Image image=tk.createImage(&"Icons/notepad.gif&"); this.setIconImage(image);//设置窗口在屏幕上的位置、大小和可见性this.setLocation(150,150);this.setSize(650,550);this.setVisible(true);checkMenuItemEnabled();editArea.requestFocus();} //设置菜单项的可用性:剪切,复制,粘贴,删除功能 public void checkMenuItemEnabled(){String selectText=editArea.getSelectedText();if(selectText==null){ editMenu_Cut.setEnabled(false);popupMenu_Cut.setEnabled(false);cutButton.setEnabled(false);editMenu_Copy.setEnabled(false);popupMenu_Copy.setEnabled(false);copyButton.setEnabled(false);editMenu_Delete.setEnabled(false);popupMenu_Delete.setEnabled(false);deleteButton.setEnabled(false); }else{editMenu_Cut.setEnabled(true);popupMenu_Cut.setEnabled(true);cutButton.setEnabled(true); editMenu_Copy.setEnabled(true);popupMenu_Copy.setEnabled(true);copyButton.setEnabled(true);editMenu_Delete.setEnabled(true);popupMenu_Delete.setEnabled(true);deleteButton.setEnabled(true);}//粘贴功能可用性判断Transferable contents=clipBoard.getContents(this);if(contents==null){editMenu_Paste.setEnabled(false);popupMenu_Paste.setEnabled(false);pasteButton.setEnabled(false);}else{editMenu_Paste.setEnabled(true);popupMenu_Paste.setEnabled(true);pasteButton.setEnabled(true);} }//方法checkMenuItemEnabled()结束 & 转自:http://ass.3daixie.com/2018061616110254.html

你可能感兴趣的:(2021-04-09)