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

时间:2023-01-27 00:12:27
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. }