📄 mytexteditor.java
字号:
} } else { fileName = ""; textBox.setText(""); } changed = false; } //当应用程序被关闭时,exitForm方法被调用 private void exitForm(java.awt.event.WindowEvent evt) { int n; Object[] option = { "YES", "NO" }; if(changed == true) { n = JOptionPane.showOptionDialog(null, "你的文件还没有保存,你想现在保存吗?", "保存", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, option, option[0] ); if (n == JOptionPane.YES_OPTION) { doSave(fileName); System.exit(0); } else if (n == JOptionPane.NO_OPTION) { System.exit(0); } } } //将文本区域中的内容保存到文件 private void doSave(String fileName) { FileOutputStream fos = null; if(fileName == "") { doSaveAs(); } else { String str = textBox.getText(); try { fos = new FileOutputStream(fileName); fos.write(str.getBytes()); } catch (IOException e) { } finally { try { fos.close(); } catch (IOException e2) { } } } changed = false; } //将当前文件保存为另外一个与原来文件名不同的文件 private void doSaveAs() { FileOutputStream fos = null; String str = textBox.getText(); FileDialog fileDialog = new FileDialog(this, "Save As...", FileDialog.SAVE); fileDialog.show(); if (fileDialog.getFile() == null) { return; } fileName = fileDialog.getDirectory() + File.separator + fileDialog.getFile(); try { fos = new FileOutputStream(fileName); fos.write(str.getBytes()); } catch (IOException e) { } finally { try { fos.close(); } catch (IOException e2) { } } } //添加工具条按钮方法,该方法在工具条创建后被调用 protected void toolBaraddButtons(JToolBar toolBar) { JButton button = null; button = makeNavigationButton("new", NEW,"新建","new"); toolBar.add(button); //添加new按钮 button = makeNavigationButton("open", OPEN,"打开","open"); toolBar.add(button);//添加open按钮 button = makeNavigationButton("save", SAVE,"保存","save"); toolBar.add(button);//添加save按钮 button = makeNavigationButton("saveas", SAVE,"另存为","saveas"); toolBar.add(button);//添加saveas按钮 toolBar.addSeparator(); //添加工具条分隔条 button = makeNavigationButton("copy", COPY,"复制","copy"); toolBar.add(button); //添加copy按钮 button = makeNavigationButton("paste", PASTE,"粘贴","paste"); toolBar.add(button); //添加paste按钮 button = makeNavigationButton("cut", CUT,"剪切","cut"); toolBar.add(button);//添加cut按钮 button = makeNavigationButton("search", FIND,"查找","find"); toolBar.add(button);//添加find按钮 } //当创建工具条按钮时,makeNavigationButton方法被调用。 //各参数:imageName 在工具条上要显示的图片的名称 // actionCommand 按钮动作的标志 // toolTipText 鼠标在按钮上停放时显示的提示文本 // altText 当找不到图片路径时,显示的文字 protected JButton makeNavigationButton(String imageName, String actionCommand, String toolTipText, String altText) { String imgLocation = imageName + ".gif"; //按钮图片的称 //定位按钮图片的路径,一般情况下会在相应class文件的目录理查找 URL imageURL = MyTextEditor.class.getResource(imgLocation); JButton button = new JButton(); //创建按钮对象 button.setToolTipText(toolTipText); // 设置提示信息 //根据actionCommand信息,设置按钮的监听器 if(NEW.equals(actionCommand)) { button.addActionListener(new java.awt.event.ActionListener(){ public void actionPerformed(java.awt.event.ActionEvent evt){ newMenuItemActionPerformed(evt); } }); } else if(OPEN.equals(actionCommand)) { button.addActionListener(new java.awt.event.ActionListener(){ public void actionPerformed(java.awt.event.ActionEvent evt){ openMenuItemActionPerformed(evt); } }); } else if(SAVE.equals(actionCommand)) { button.addActionListener(new java.awt.event.ActionListener(){ public void actionPerformed(java.awt.event.ActionEvent evt){ saveMenuItemActionPerformed(evt); } }); } else if(SAVEAS.equals(actionCommand)) { button.addActionListener(new java.awt.event.ActionListener(){ public void actionPerformed(java.awt.event.ActionEvent evt){ saveAsMenuItemActionPerformed(evt); } }); } else if(COPY.equals(actionCommand)) { button.addActionListener(new java.awt.event.ActionListener(){ public void actionPerformed(java.awt.event.ActionEvent evt){ copyMenuItemActionPerformed(evt); } }); } else if(PASTE.equals(actionCommand)) { button.addActionListener(new java.awt.event.ActionListener(){ public void actionPerformed(java.awt.event.ActionEvent evt){ pasteMenuItemActionPerformed(evt); } }); } else if(CUT.equals(actionCommand)) { button.addActionListener(new java.awt.event.ActionListener(){ public void actionPerformed(java.awt.event.ActionEvent evt){ cutMenuItemActionPerformed(evt); } }); } else if(FIND.equals(actionCommand)) { button.addActionListener(new java.awt.event.ActionListener(){ public void actionPerformed(java.awt.event.ActionEvent evt){ findMenuItemActionPerformed(evt); } }); } //为按钮设置图片 if (imageURL != null) { button.setIcon(new ImageIcon(imageURL)); } else { button.setText(altText); System.err.println("Resource not found: "+ imgLocation); } return button; } // TextEdit类的的成员变量定义 //GUI组件声名开始 private javax.swing.JMenuItem aboutMenuItem; private javax.swing.JMenu editMenu; private javax.swing.JMenuItem exitMenuItem; private javax.swing.JMenu fileMenu; private javax.swing.JMenuItem copyMenuItem; private javax.swing.JMenuItem pasteMenuItem; private javax.swing.JMenuItem cutMenuItem; private javax.swing.JMenuItem findMenuItem; private javax.swing.JMenu helpMenu; private javax.swing.JSeparator jSeparator1; private javax.swing.JSeparator jSeparator2; private javax.swing.JSeparator jSeparator3; private javax.swing.JMenuItem newMenuItem; private javax.swing.JMenuItem openMenuItem; private javax.swing.JMenuItem saveAsMenuItem; private javax.swing.JMenuItem saveMenuItem; private javax.swing.JMenu setMenu; private javax.swing.JMenuItem FcolorMenuItem; private javax.swing.JMenuItem BcolorMenuItem; private javax.swing.JMenuBar tedMenuBar; private javax.swing.JTextArea textBox; private javax.swing.JScrollPane textScrollPane; private javax.swing.JToolBar toolBar; //GUI组件声名结束 private String fileName = ""; private String COPY = "copy"; private String CUT = "cut"; private String PASTE = "paste"; private String NEW = "new"; private String OPEN = "open"; private String SAVE = "save"; private String SAVEAS = "saveas"; private String FIND = "find"; private boolean changed; //应用程序的main函数 public static void main(java.lang.String[] args) { new MyTextEditor().show(); }//end main //弹出式菜单(PopupMenu)事件的适配器 class PopupListener extends MouseAdapter { PopupListener(JPopupMenu popupMenu) { popup = popupMenu; } public void mousePressed(MouseEvent e) { maybeShowPopup(e); } public void mouseReleased(MouseEvent e) { maybeShowPopup(e); } private void maybeShowPopup(MouseEvent e) { if (e.isPopupTrigger()) { popup.show(e.getComponent(), e.getX(), e.getY()); } } JPopupMenu popup; }// class PopupListener结束}//class MyTextEditor结束
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -