引用页_初学Java:仿写记事本_NoteMenu.java

原创 2012年03月22日 20:20:35

<< 返回
  

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.Calendar;

import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.KeyStroke;

@SuppressWarnings("serial")
public class NoteMenu extends JMenuBar {
	private Notepad notepad = null;
	
	private JMenu file = null;			//文件菜单
	private JMenu edit = null;			//编辑菜单
	private JMenu option = null;		//格式菜单
	private JMenu view = null;			//查看
	private JMenu help = null;			//帮助菜单
	
	private JMenuItem file_New = null;			//新建
	private JMenuItem file_Open = null;			//打开
	private JMenuItem file_Save = null;			//保存
	private JMenuItem file_SaveAnother = null;	//另存为
	private JMenuItem file_Settings = null;		//页面设置
	private JMenuItem file_Print = null;		//打印
	private JMenuItem file_Exit = null;			//退出
	
	private JMenuItem edit_Z = null;			//撤销
	private JMenuItem edit_Y = null;			//恢复
	private JMenuItem edit_X = null;			//剪切
	private JMenuItem edit_C = null;			//复制
	private JMenuItem edit_V = null;			//粘贴
	private JMenuItem edit_Del = null;			//删除
	private JMenuItem edit_Find = null;			//查找
	private JMenuItem edit_FindNext = null;		//查找下一个
	private JMenuItem edit_Replace = null;		//替换
	private JMenuItem edit_Go = null;			//转到
	private JMenuItem edit_All = null;			//全选
	private JMenuItem edit_Date = null;			//日期/时间
	
	private JCheckBoxMenuItem option_Enter = null;		//自动换行
	private JMenuItem option_Font = null;		//字体
	
	private JCheckBoxMenuItem view_State = null;//状态栏
	
	private JMenuItem help_Read = null;			//查看帮助
	private JMenuItem help_About = null;		//关于记事本
	
	public NoteMenu(Notepad notepad) {
		this.notepad = notepad;
		this.getNew();	//实例化每一个JMenu和JMenuItem
		this.addMenu();	//为JMenu和JMenuItem设置布局
		this.addEvent();//添加事件监听
	}
	
	private void getNew() {
		this.file = new JMenu("文件(F)");
		this.file.setMnemonic('F');
		this.edit = new JMenu("编辑(E)");
		this.edit.setMnemonic('E');
		this.option = new JMenu("格式(O)");
		this.option.setMnemonic('O');
		this.view = new JMenu("查看(V)");
		this.view.setMnemonic('V');
		this.help = new JMenu("帮助(H)");
		this.help.setMnemonic('H');
		
		this.file_New = new JMenuItem("新建(N)");
		this.file_New.setMnemonic('N');				//设置快捷键为 N
		this.file_New.setAccelerator(KeyStroke.getKeyStroke('N', java.awt.Event.CTRL_MASK));	// Ctrl + N
		this.file_Open = new JMenuItem("打开(O)...");
		this.file_Open.setMnemonic('O');
		this.file_Open.setAccelerator(KeyStroke.getKeyStroke('O', java.awt.Event.CTRL_MASK));
		this.file_Save = new JMenuItem("保存(S)");
		this.file_Save.setMnemonic('S');
		this.file_Save.setAccelerator(KeyStroke.getKeyStroke('S', java.awt.Event.CTRL_MASK));
		this.file_SaveAnother = new JMenuItem("另存为(A)...");
		this.file_SaveAnother.setMnemonic('A');
		this.file_Settings = new JMenuItem("页面设置(U)...");
		this.file_Settings.setMnemonic('U');
		this.file_Settings.setEnabled(false);
		this.file_Print = new JMenuItem("打印(P)...");
		this.file_Print.setMnemonic('P');
		this.file_Print.setEnabled(false);
		this.file_Print.setAccelerator(KeyStroke.getKeyStroke('P', java.awt.Event.CTRL_MASK));
		this.file_Exit = new JMenuItem("退出(X)");
		this.file_Exit.setMnemonic('X');
		
		this.edit_Z = new JMenuItem("撤消(U)");
		this.edit_Z.setMnemonic('U');
		this.edit_Z.setAccelerator(KeyStroke.getKeyStroke('Z', java.awt.Event.CTRL_MASK));
		this.edit_Z.setEnabled(false);
		this.edit_Y = new JMenuItem("恢复(Y)");
		this.edit_Y.setMnemonic('Y');
		this.edit_Y.setAccelerator(KeyStroke.getKeyStroke('Y', java.awt.Event.CTRL_MASK));
		this.edit_Y.setEnabled(false);
		this.edit_X = new JMenuItem("剪切(T)");
		this.edit_X.setMnemonic('T');
		this.edit_X.setAccelerator(KeyStroke.getKeyStroke('X', java.awt.Event.CTRL_MASK));
		this.edit_X.setEnabled(false);
		this.edit_C = new JMenuItem("复制(C)");
		this.edit_C.setMnemonic('C');
		this.edit_C.setAccelerator(KeyStroke.getKeyStroke('C', java.awt.Event.CTRL_MASK));
		this.edit_C.setEnabled(false);
		this.edit_V = new JMenuItem("粘贴(P)");
		this.edit_V.setMnemonic('P');
		this.edit_V.setAccelerator(KeyStroke.getKeyStroke('V', java.awt.Event.CTRL_MASK));
		this.edit_V.setEnabled(false);
		this.edit_Del = new JMenuItem("删除(L)");
		this.edit_Del.setMnemonic('L');
		this.edit_Del.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0));
		this.edit_Del.setEnabled(false);
		this.edit_Find = new JMenuItem("查找(F)...");
		this.edit_Find.setMnemonic('F');
		this.edit_Find.setAccelerator(KeyStroke.getKeyStroke('F', java.awt.Event.CTRL_MASK));
		this.edit_Find.setEnabled(false);
		this.edit_FindNext = new JMenuItem("查找下一个(N)");
		this.edit_FindNext.setMnemonic('N');
		this.edit_FindNext.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F3, 0));
		this.edit_FindNext.setEnabled(false);
		this.edit_Replace = new JMenuItem("替换(R)...");
		this.edit_Replace.setMnemonic('R');
		this.edit_Replace.setAccelerator(KeyStroke.getKeyStroke('H', java.awt.Event.CTRL_MASK));
		this.edit_Replace.setEnabled(true);
		this.edit_Go = new JMenuItem("转到(G)...");
		this.edit_Go.setMnemonic('G');
		this.edit_Go.setAccelerator(KeyStroke.getKeyStroke('G', java.awt.Event.CTRL_MASK));
		this.edit_All = new JMenuItem("全选(A)");
		this.edit_All.setMnemonic('A');
		this.edit_All.setAccelerator(KeyStroke.getKeyStroke('A', java.awt.Event.CTRL_MASK));
		this.edit_Date = new JMenuItem("日期/时间(D)");
		this.edit_Date.setMnemonic('D');
		this.edit_Date.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0));
		
		this.option_Enter = new JCheckBoxMenuItem("自动换行(W)");
		this.option_Enter.setSelected(this.notepad.getTextArea().getLineWrap());
		this.option_Font = new JMenuItem("字体(F)...");
		this.option_Font.setMnemonic('F');
		
		this.view_State = new JCheckBoxMenuItem("状态栏(S)");
		this.view_State.setMnemonic('S');
		
		this.help_Read = new JMenuItem("查看帮助(H)");
		this.help_Read.setMnemonic('H');
		this.help_Read.setEnabled(false);
		this.help_About = new JMenuItem("关于记事本(A)");
		this.help_About.setMnemonic('A');
	}
	
	private void addMenu() {
		this.add(this.file);
		this.add(this.edit);
		this.add(this.option);
		this.add(this.view);
		this.add(this.help);
		
		this.file.add(this.file_New);
		this.file.add(this.file_Open);
		this.file.add(this.file_Save);
		this.file.add(this.file_SaveAnother);
		this.file.addSeparator();
		this.file.add(this.file_Settings);
		this.file.add(this.file_Print);
		this.file.addSeparator();
		this.file.add(this.file_Exit);
		
		this.edit.add(this.edit_Z);
		this.edit.add(this.edit_Y);
		this.edit.addSeparator();
		this.edit.add(this.edit_X);
		this.edit.add(this.edit_C);
		this.edit.add(this.edit_V);
		this.edit.add(this.edit_Del);
		this.edit.addSeparator();
		this.edit.add(this.edit_Find);
		this.edit.add(this.edit_FindNext);
		this.edit.add(this.edit_Replace);
		this.edit.add(this.edit_Go);
		this.edit.addSeparator();
		this.edit.add(this.edit_All);
		this.edit.add(this.edit_Date);
		
		this.option.add(this.option_Enter);
		this.option.add(this.option_Font);
		
		this.view.add(this.view_State);
		
		this.help.add(this.help_Read);
		this.help.addSeparator();
		this.help.add(this.help_About);
	}
	
	private void addEvent() {
		this.file_New.addActionListener(new ActionListener(){
					public void actionPerformed(ActionEvent e){
						notepad.createFile();
					}
				});
		
		this.file_Open.addActionListener(new ActionListener(){
					public void actionPerformed(ActionEvent e){
						notepad.openFile();
					}
				});
		
		this.file_Save.addActionListener(new ActionListener(){
					public void actionPerformed(ActionEvent e){
						notepad.saveFile();
					}
				});
		
		this.file_SaveAnother.addActionListener(new ActionListener(){
					public void actionPerformed(ActionEvent e){
						notepad.saveAnother();
					}
				});
		
		this.file_Exit.addActionListener(new ActionListener(){
					public void actionPerformed(ActionEvent e){
						notepad.windowExit();
					}
				});
		
		this.edit_Z.addActionListener(new ActionListener(){
					public void actionPerformed(ActionEvent e){
						if (notepad.getUndomg().canUndo()) {
							notepad.getUndomg().undo();
							setItem("UNDO", notepad.getUndomg().canUndo());
							setItem("REDO", notepad.getUndomg().canRedo());
						}
					}
				});
		
		this.edit_Y.addActionListener(new ActionListener(){
					public void actionPerformed(ActionEvent e){
						if (notepad.getUndomg().canRedo()) {
							notepad.getUndomg().redo();
							setItem("UNDO", notepad.getUndomg().canUndo());
							setItem("REDO", notepad.getUndomg().canRedo());
						}
					}
				});
		
		this.edit_X.addActionListener(new ActionListener(){
					public void actionPerformed(ActionEvent e){
						notepad.getTextArea().cut();
					}
				});
		
		this.edit_C.addActionListener(new ActionListener(){
					public void actionPerformed(ActionEvent e){
						notepad.getTextArea().copy();
					}
				});
		
		this.edit_V.addActionListener(new ActionListener(){
					public void actionPerformed(ActionEvent e){
						notepad.getTextArea().paste();
					}
				});
		
		this.edit_Find.addActionListener(new ActionListener(){
					public void actionPerformed(ActionEvent e){
						new Find(notepad, true);
					}
				});
		
		this.edit_FindNext.addActionListener(new ActionListener(){
					public void actionPerformed(ActionEvent e){
						if (notepad.getFindStr() != null && !("".equals(notepad.getFindStr()))) {
							new Find(notepad, false).autoFindNext();
						} else {
							new Find(notepad, true);
						}
					}
				});
		
		this.edit_Del.addActionListener(new ActionListener(){
					public void actionPerformed(ActionEvent e){
						System.out.println("当前位置: " + notepad.getTextArea().getCaretPosition());
						notepad.getTextArea().replaceRange("", notepad.getTextArea().getSelectionStart(), notepad.getTextArea().getSelectionEnd());
					}
				});
		
		this.edit_Replace.addActionListener(new ActionListener(){
					public void actionPerformed(ActionEvent e){
						new Replace(notepad);
					}
				});
		
		this.edit_Go.addActionListener(new ActionListener(){
					public void actionPerformed(ActionEvent e){
						new GoDialog(notepad);
					}
				});
		
		this.edit_All.addActionListener(new ActionListener(){
					public void actionPerformed(ActionEvent e){
						notepad.getTextArea().selectAll();
					}
				});
		
		this.edit_Date.addActionListener(new ActionListener(){	//当前位置插入时间/日期
					public void actionPerformed(ActionEvent e){
						Calendar time = Calendar.getInstance();
						String str = time.get(Calendar.HOUR_OF_DAY) + ":" + time.get(Calendar.MINUTE) + " " + time.get(Calendar.YEAR) 
								+ "/" + (time.get(Calendar.MONTH) + 1) + "/" + time.get(Calendar.DAY_OF_MONTH);
						notepad.getTextArea().insert(str, notepad.getTextArea().getCaretPosition());
					}
				});
		
		this.option_Enter.addActionListener(new ActionListener(){
					public void actionPerformed(ActionEvent e){
						if(notepad.getTextArea().getLineWrap()) {
							notepad.getTextArea().setLineWrap(false);
							notepad.getScrollPane().setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
						} else {
							notepad.getTextArea().setLineWrap(true);
							notepad.getScrollPane().setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
						}
						if(option_Enter.isSelected()) {// 如果状态栏可见
							notepad.getStatus().setVisible(false);	//设置它不可见
							view_State.setEnabled(false);	//菜单不可用
						} else {
							view_State.setEnabled(true);	//菜单可用
							if(view_State.isSelected()) {	//如果菜单是选中的
								notepad.getStatus().setVisible(true);
							}
						}
						if(option_Enter.isSelected()) {
							edit_Go.setEnabled(false);
						} else {
							edit_Go.setEnabled(true);
						}
					}
				});
		
		this.option_Font.addActionListener(new ActionListener(){	//设置字体
					public void actionPerformed(ActionEvent e){
						new NoteFont(notepad);
					}
				});
		
		this.view_State.addActionListener(new ActionListener(){
					public void actionPerformed(ActionEvent e){
						if(notepad.getStatus().isVisible()) {
							notepad.getStatus().setVisible(false);
						} else {
							notepad.getStatus().setVisible(true);
						}
					}
				});
		
		this.help_About.addActionListener(new ActionListener(){
					public void actionPerformed(ActionEvent e){
						new MenuAbout();
					}
				});
	}
	
	public void setItem(String name, boolean whether) {
		if("DEL".equals(name)) {
			this.edit_Del.setEnabled(whether);
		} else if ("CUT".equals(name)) {
			this.edit_X.setEnabled(whether);
		} else if ("COPY".equals(name)) {
			this.edit_C.setEnabled(whether);
		} else if ("PASTE".equals(name)) {
			this.edit_V.setEnabled(whether);
		} else if ("FIND".equals(name)) {
			this.edit_Find.setEnabled(whether);
		} else if ("FIND_NEXT".equals(name)) {
			this.edit_FindNext.setEnabled(whether);
		} else if ("UNDO".equals(name)) {
			this.edit_Z.setEnabled(whether);
		} else if ("REDO".equals(name)) {
			this.edit_Y.setEnabled(whether);
		} else {
			System.out.println("发生异常!\nNoteMenu.java: setItem(String, boolean)");
		}
	}
}

  
<< 返回

相关文章推荐

引用页_初学Java:仿写记事本_OpenFile.java

返回    import java.io.File; import java.io.FileReader; import java.io.Reader; import javax.swing...
  • Tieper
  • Tieper
  • 2012年03月22日 20:23
  • 294

引用页_初学Java:仿写记事本_Notepad.java

返回    import java.awt.BorderLayout; import java.awt.Font; import java.awt.event.ActionEvent; impor...
  • Tieper
  • Tieper
  • 2012年03月22日 20:22
  • 305

引用页_初学Java:仿写记事本_TextKeyListener.java

返回    /** * 为了状态栏功能而设立的监听类 * 第一次改写: * 同时也适用于对“编辑”-“删除”菜单的可用设置 * 第二次改写: * 同时也适用于对“格式”-“字体”...
  • Tieper
  • Tieper
  • 2012年03月22日 20:31
  • 257

引用页_初学Java:仿写记事本_SaveFile.java

返回    import java.io.File; import java.io.FileOutputStream; import java.io.PrintStream; import ja...
  • Tieper
  • Tieper
  • 2012年03月22日 20:28
  • 170

引用页_初学Java:仿写记事本_GoDialog.java

返回    import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListene...
  • Tieper
  • Tieper
  • 2012年03月22日 20:12
  • 194

引用页_初学Java:仿写记事本_Find.java

返回    import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListene...
  • Tieper
  • Tieper
  • 2012年03月22日 20:08
  • 204

引用页_初学Java:仿写记事本_TextMouseListener.java

返回    /** * 为了状态栏功能而设立的监听类 * 第一次改写: * 同时也适用于对“编辑”-“删除”菜单的可用设置 * 第二次改写: * 同时也适用于对“格式”-“字体”...
  • Tieper
  • Tieper
  • 2012年03月22日 20:32
  • 166

初学Java:仿写记事本

一、前言      纸上得来终觉浅,绝知此事要躬行。   自学Java已经有一段时间了,最近读完了李兴华老师的《Java开发实战经典》一书,受益匪浅。感觉其中的知识比较全面,实用性也比较高,为了...
  • Tieper
  • Tieper
  • 2012年03月22日 19:52
  • 419

Java Swing写的字体选择器,仿记事本中的字体控件

Java Swing 写的字体选择器,仿记事本中的字体控件,使用操作方法与文件选择器JFileChooser基本相同。   import java.awt.BorderLayout; i...
  • ycb1689
  • ycb1689
  • 2012年12月19日 09:15
  • 1975

java写的仿windos的记事本

  • 2010年02月27日 21:01
  • 464KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:引用页_初学Java:仿写记事本_NoteMenu.java
举报原因:
原因补充:

(最多只允许输入30个字)