【Java】javax.swing.JMenu 练习

时间:2023-01-26 18:46:00
 
 

设计一个窗口,窗口中心添加一个JTextArea,另添加4个菜单,每个菜单都有菜单项,每个菜单项都对应有快捷键,选择某个菜单项时,窗口中心的文本区都显示对应的信息。

  1 import java.awt.event.ActionEvent;
  2 import java.awt.event.ActionListener;
  3 import java.awt.event.KeyEvent;
  4 
  5 import javax.swing.JMenuBar;
  6 import javax.swing.JMenu;
  7 import javax.swing.JMenuItem;
  8 import javax.swing.JOptionPane;
  9 import javax.swing.JFrame;
 10 import javax.swing.JTextArea;
 11 import javax.swing.JScrollPane;
 12 import java.awt.MenuShortcut;
 13 
 14 class ExMyFrame66 extends JFrame implements ActionListener {
 15     JTextArea tArea;
 16     JScrollPane scrollpane;
 17     
 18     JMenuBar menubar ; //菜单栏
 19     JMenu menuFile; //文件菜单
 20     JMenu menuEdit; //编辑菜单
 21     JMenu menuWin; //窗口菜单
 22     JMenuItem menuItemOpen; //打开
 23     JMenuItem menuItemSave; //保存
 24     JMenuItem menuItemNew;  //新建
 25     JMenuItem menuItemCopy; //复制
 26     JMenuItem menuItemPaste; //粘贴
 27     JMenuItem menuItemCut; //剪切
 28     JMenuItem menuItemAbout;  //关于
 29     JMenuItem menuItemExit; //退出
 30     
 31     MenuShortcut mShorcutFile;
 32     MenuShortcut mShorcutEdit;
 33     MenuShortcut mShorcutWin;
 34     MenuShortcut mShorcutNew;
 35     MenuShortcut mShorcutOpen;
 36     MenuShortcut mShorcutSave;
 37     MenuShortcut mShorcutCut;
 38     MenuShortcut mShorcutCopy;
 39     MenuShortcut mShorcutPaste;
 40     MenuShortcut mShorcutAbout;
 41     MenuShortcut mShorcutExit;
 42     
 43     ExMyFrame66(){
 44         setSize(400,300);
 45         setTitle("菜单栏练习");
 46         //初始化文本区、滚动面板
 47         tArea = new JTextArea("文本编辑区");
 48         tArea.setEditable(true);
 49         scrollpane = new JScrollPane(tArea);
 50         this.add(scrollpane);
 51         
 52         //初始化菜单栏、菜单、菜单项、菜单项快捷键
 53         menubar = new JMenuBar();
 54         menuFile = new JMenu("文件");
 55         menuEdit = new JMenu("编辑");
 56         menuWin = new JMenu("窗口");
 57         
 58         menuItemNew = new JMenuItem("新建") ;
 59         menuItemOpen = new JMenuItem("打开") ;
 60         menuItemSave = new JMenuItem("保存") ;
 61         menuItemCut = new JMenuItem("剪切") ;
 62         menuItemCopy = new JMenuItem("复制") ;
 63         menuItemPaste = new JMenuItem("粘贴") ;
 64         menuItemAbout = new JMenuItem("关于") ;
 65         menuItemExit = new JMenuItem("退出") ;
 66         
 67         super.setJMenuBar(menubar); // 将菜单栏添加到JFrame(super)或者JFrame的子类也即“ExMyFrame66”
 68         
 69         // 将菜单添加到菜单栏
 70         menubar.add(menuFile);
 71         menubar.add(menuEdit);
 72         menubar.add(menuWin);
 73         
 74         // 将菜单项添加到菜单
 75         menuFile.add(menuItemNew);
 76         menuFile.add(menuItemOpen);
 77         menuFile.add(menuItemSave);
 78         menuEdit.add(menuItemCut);
 79         menuEdit.add(menuItemCopy);
 80         menuEdit.add(menuItemPaste);
 81         menuWin.add(menuItemAbout);
 82         menuWin.add(menuItemExit);
 83         
 84         //将菜单及菜单项快捷键绑定到菜单与菜单项上
 85         mShorcutFile = new MenuShortcut(KeyEvent.VK_F);
 86         mShorcutEdit = new MenuShortcut(KeyEvent.VK_E);
 87         mShorcutWin = new MenuShortcut(KeyEvent.VK_W);
 88         mShorcutOpen = new MenuShortcut(KeyEvent.VK_O);
 89         mShorcutNew = new MenuShortcut(KeyEvent.VK_N);
 90         mShorcutSave = new MenuShortcut(KeyEvent.VK_S);
 91         mShorcutCopy = new MenuShortcut(KeyEvent.VK_C);
 92         mShorcutCut = new MenuShortcut(KeyEvent.VK_X);
 93         mShorcutPaste = new MenuShortcut(KeyEvent.VK_V);
 94         mShorcutAbout = new MenuShortcut(KeyEvent.VK_A);
 95         mShorcutExit = new MenuShortcut(KeyEvent.VK_Q);
 96         
 97         
 98         //注册菜单项监听器
 99         //menuFile.addActionListener(this);  //菜单Menu不能添加事件触发器,只有菜单项MenuItem才能添加ActionListener触发器
100         //menuEdit.addActionListener(this);
101         //menuWin.addActionListener(this);
102         menuItemNew.addActionListener(this);
103         menuItemOpen.addActionListener(this);
104         menuItemSave.addActionListener(this);
105         menuItemCut.addActionListener(this);
106         menuItemCopy.addActionListener(this);
107         menuItemPaste.addActionListener(this);
108         menuItemAbout.addActionListener(this);
109         menuItemExit.addActionListener(this);
110 
111     }
112     
113     @Override
114     public void actionPerformed(ActionEvent e) {
115         // TODO Auto-generated method stub
116         //如果ActionListener触发的事件来源是菜单或菜单项,则显示菜单项的名称
117         if (e.getSource().getClass() == (new JMenu()).getClass() || 
118             e.getSource().getClass() == (new JMenuItem()).getClass()){
119             tArea.setText("选择了"+e.getActionCommand()); 
120         }
121         
122         //设置“退出”菜单项的事件解释
123         if (e.getSource() == menuItemExit){
124             int result = JOptionPane.showConfirmDialog(this, "确定要退出吗?","退出确认",JOptionPane.YES_NO_OPTION);//this代表对话框依赖的父窗口
125             if (result == JOptionPane.YES_OPTION){
126                 System.exit(0);
127             }
128         }
129     }
130 
131 }