当前位置: 代码迷 >> J2SE >> java 菜单,以字符数组的方式添加菜单项时怎么给菜单项添加事件处理
  详细解决方案

java 菜单,以字符数组的方式添加菜单项时怎么给菜单项添加事件处理

热度:2393   发布时间:2013-02-25 00:00:00.0
java 菜单,以字符数组的方式添加菜单项时如何给菜单项添加事件处理
例如:
JMenuItem item1;
JMenuItem item2;
JMenuItem item3;
JMenuItem item4;
JMenuItem item5;
String[] str1 = { "关于系统","退出系统" };
String[] str2 = { "增加用户", "删除用户", "修改用户", "查看系统用户" };
String[] str3 = { "进入管理系统", "生成报表"};
String[] str4 = { "进入购物系统"};
String[] str5 = { "游客注册","用户指南"};
for (int i = 0; i < str1.length; i++) {
item1 = new JMenuItem(str1[i]);
menu1.add(item1);
}
for (int i = 0; i < str2.length; i++) {
item2 = new JMenuItem(str2[i]);
menu2.add(item2);
}
for (int i = 0; i < str3.length; i++) {
item3 = new JMenuItem(str3[i]);
menu3.add(item3);
}
for (int i = 0; i < str4.length; i++) {
item4 = new JMenuItem(str4[i]);
menu4.add(item4);
}
for (int i = 0; i < str5.length; i++) {
item5 = new JMenuItem(str5[i]);
menu5.add(item5);
}
如果以上面的方式创建菜单项,如何给菜单项添加事件处理?

------解决方案--------------------------------------------------------
应该使用Action类,既可以添加到菜单有可以添加到工具条,还可以用来新建JButton。可以当作ActionListener使用。
------解决方案--------------------------------------------------------
我程序里面的一段你应该能看懂:
MenuItem exitItem = new MenuItem(" 退出程序 ");
exitItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (JOptionPane.showConfirmDialog(null, "确定退出系统") == 0) {
System.exit(0);
}
}
------解决方案--------------------------------------------------------
Java code
public void actionPerformed(ActionEvent e) {        String cmd = e.getActionCommand();        if ("退出".equals(cmd)) {            if (wasSaved == false) {                int op = JOptionPane.showConfirmDialog(jf, "文件没保存是否退出?",                        "确认退出", JOptionPane.YES_NO_CANCEL_OPTION);                if (op != JOptionPane.YES_OPTION) {                    return;                }            }            System.exit(0);        }        if ("打开".equals(cmd)) {            FileDialog fd = new FileDialog(jf, "打开文件");            fd.setVisible(true);            filePath = fd.getDirectory() + fd.getFile();            BufferedReader br = null;            try {                br = new BufferedReader(new InputStreamReader(                        new FileInputStream(filePath), "utf-8"));                                String str;                while((str=br.readLine())!=null){                                        jta.append(str+"\n");                                    }                            } catch (Exception ex) {                ex.printStackTrace();            } finally {                if(br!=null){                    try {                        br.close();                    } catch (IOException e1) {                        e1.printStackTrace();                    }                }            }            wasSaved=true;        }        if ("保存".equals(cmd)) {            if(filePath==null){                FileDialog fd=new FileDialog(jf);                fd.setVisible(true);                filePath=fd.getDirectory()+fd.getFile();            }                        save();            wasSaved=true;                                }        if ("另存为".equals(cmd)) {            FileDialog fd=new FileDialog(jf);            fd.setVisible(true);            filePath=fd.getDirectory()+fd.getFile();            save();            wasSaved=true;        }    }
  相关解决方案