<< 返回
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)");
}
}
}
<< 返回