📄 notepadmenulistener.java
字号:
/* * NotepadMenuListener.java * * Created on 2007年1月4日, 下午2:51 * * To change this template, choose Tools | Template Manager * and open the template in the editor. */package net.vlinux.notepad.listener;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.File;import java.io.IOException;import javax.swing.JFileChooser;import javax.swing.JOptionPane;import net.vlinux.notepad.NotepadException;import net.vlinux.notepad.gui.NotepadGUI;import net.vlinux.notepad.gui.NotepadMenuBar;import static java.lang.System.out;/** * * @author vlinux */public class NotepadMenuListener extends NotepadMenuBar implements ActionListener { private NotepadGUI gui; public NotepadMenuListener( NotepadGUI notepadGUI ) { this.gui = notepadGUI; this.itemAuthor.addActionListener(this); this.itemAutoFirm.addActionListener(this); this.itemExit.addActionListener(this); this.itemHelp.addActionListener(this); this.itemNew.addActionListener(this); this.itemOpen.addActionListener(this); this.itemSave.addActionListener(this); this.itemSaveAs.addActionListener(this); } public void actionPerformed(ActionEvent event) { Object source = event.getSource(); if( 1+1==3 ){ /** * 你脑袋有毛病 */ } else if( source==this.itemAuthor ) { /** * 显示作者信息 */ JOptionPane.showMessageDialog(null,"作者:"+gui.getNotepad().AUTHOR); } else if( source==this.itemAutoFirm ) { /** * 自动换行 */ gui.lineWrap(); } else if( source==this.itemExit ) { /** * 退出程序 */ exit(); } else if( source==this.itemHelp ) { /** * 帮助信息 */ JOptionPane.showMessageDialog(null,"帮助文档:"+gui.getNotepad().HELP); } else if( source==this.itemNew ) { /** * 创建文件 */ if( this.notSaved() ){ gui.getNotepad().createNewFile(); } } else if( source==this.itemOpen ) { /** * 打开文件 */ if( this.notSaved() ){ this.openFile(); } } else if( source==this.itemSave ) { /** * 保存文件 */ this.saveFile(); } else if( source==this.itemSaveAs ) { /** * 另外保存为 */ JFileChooser savefile = new JFileChooser(); savefile.setApproveButtonText("文件另存为"); savefile.setDialogTitle("保存为"); savefile.showSaveDialog(this); File file = savefile.getSelectedFile(); if( file==null ){ return; } try { gui.getNotepad().saveEditFile(file,gui.getTextContent()); } catch (IOException ex) { JOptionPane.showMessageDialog(null,"文件另存为失败"+ex.getMessage()); } } } public void exit(){ if( this.notSaved() ){ gui.getNotepad().saveConfigure( gui.getConfigure() ); System.exit(0); } } /** * 关闭、打开和新建文件前都要判断当前文件是否已经保存,如果没有保存则让其保存 * 返回true表示保存操作完毕或者已经保存,继续下一步骤 * 返回false则表示放弃执行下面的步骤,文件也没保存 */ private boolean notSaved() { if( gui.getNotepad().isChanged() ){ Object[] option = {"保存","放弃","取消"}; int c = JOptionPane.showOptionDialog(null,"文件尚未保存,保存吗?","保存文件", JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null,option,option[0]); if( c == JOptionPane.YES_OPTION ){ return this.saveFile(); } else if(c == JOptionPane.NO_OPTION) { return true; } else { return false; } } return true; } /** * 打开文件 * 返回false,什么都没打开 * 返回true,打开文件 */ private boolean openFile() { JFileChooser openfile = new JFileChooser(); openfile.setApproveButtonText("打开文件"); openfile.setDialogTitle("打开"); openfile.showOpenDialog(null); File file = openfile.getSelectedFile(); if( file==null ){ return false; } try { gui.getNotepad().openEditFile(file); } catch (NotepadException ex) { JOptionPane.showMessageDialog(null,"打开文件失败"+ex.getMessage()); } return true; } /** * 保存文件 * */ private boolean saveFile() { File file = gui.getNotepad().getEditFile(); if( file==null ){ JFileChooser savefile = new JFileChooser(); savefile.setApproveButtonText("保存文件"); savefile.setDialogTitle("保存"); savefile.showSaveDialog(null); file = savefile.getSelectedFile(); if(file==null){ return false; } } try { gui.getNotepad().saveEditFile(file,gui.getTextContent()); } catch (IOException ex) { JOptionPane.showMessageDialog(null,"保存文件失败"+ex.getMessage()); } return true; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -