javax.swing.JMenuBar JMenu 以及事件响应

Code:
  1. import java.util.*;   
  2. import java.io.*;   
  3. import java.awt.*;   
  4. import java.awt.event.*;   
  5. import javax.swing.*;   
  6.   
  7. public class TestMenu {   
  8.     public static void main(String[] args) {   
  9.         MainFrame mf = new MainFrame("测试菜单");   
  10.     }   
  11. }   
  12.   
  13. class MainFrame implements ActionListener {   
  14.     Container p;   
  15.     JMenuBar mb;   
  16.     final JMenu[] m = {new JMenu("文件"),   
  17.                                    new JMenu("编辑"),   
  18.                                    new JMenu("帮助")};   
  19.     final JMenuItem[][] mi = {{new JMenuItem("新建"), new JMenuItem("打开"), new JMenuItem("保存")},   
  20.                                                   {new JMenuItem("选色"), new JMenuItem("粘贴")},   
  21.                                                   {new JMenuItem("关于")}};   
  22.                                                    
  23.     public MainFrame(String title) {   
  24.         JFrame f = new JFrame(title);   
  25.         p = f.getContentPane();   
  26.         mb = new JMenuBar();   
  27.         for(int i=0; i<m.length; i++) {   
  28.             for(int j=0; j<mi[i].length; j++) {   
  29.                 m[i].add(mi[i][j]);   
  30.                 mi[i][j].addActionListener(this);   
  31.             }   
  32.             mb.add(m[i]);   
  33.         }   
  34.         f.setJMenuBar(mb);   
  35.         f.setBounds(200200500500);   
  36.         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
  37.         f.setVisible(true);   
  38.     }   
  39.        
  40.     public void actionPerformed(ActionEvent e) {   
  41.         if(e.getActionCommand() == "新建") {   
  42.         }   
  43.         if(e.getActionCommand() == "打开") {   
  44.             JFileChooser chooseFile = new JFileChooser();   
  45.             int returnVal = chooseFile.showOpenDialog(null);   
  46.       if(returnVal == chooseFile.APPROVE_OPTION) {   
  47.         File f = chooseFile.getSelectedFile();   
  48.         JOptionPane.showConfirmDialog(null"你选择的文件名是:"+chooseFile.getName(f),   
  49.                                       "确认",JOptionPane.ERROR_MESSAGE);   
  50.         }   
  51.         }   
  52.         if(e.getActionCommand() == "保存") {   
  53.             JFileChooser chooseFile = new JFileChooser();   
  54.             int returnVal = chooseFile.showSaveDialog(null);   
  55.       if(returnVal == chooseFile.APPROVE_OPTION) {   
  56.         File f = chooseFile.getSelectedFile();   
  57.        JOptionPane.showConfirmDialog(null,chooseFile.getName(f));   
  58.       }   
  59.         }   
  60.         if(e.getActionCommand() == "选色") {   
  61.             Color tmpColor = JColorChooser.showDialog(null"调色板", Color.red);   
  62.         }   
  63.         if(e.getActionCommand() == "粘贴") {   
  64.         }   
  65.         if(e.getActionCommand() == "关于") {   
  66.             JOptionPane.showMessageDialog(null, (Object)"http://student.csdn.net/space.php?do=home");   
  67.         }   
  68.     }   
  69. }  

 

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值