?? 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 + -