我可以从JMenu(而不是JMenuItem)获取动作命令吗?

时间:2014-12-10 10:39:13

标签: java swing actionlistener jmenu

问题是我创建了一个弹出菜单供人们选择他来自哪里,如下所示: 更改位置 - > country-> city-> district-> town。 对于最后一级城镇,它是JMenuItem所以它很容易使用getActionCommand()(我将位置ID发送到ActionCommand),但有时人们只想从城市级别或区域级别选择,addActionListener()无法帮助我getActionCommand( )。无论如何,当用户选择它时,是否有getActionCommand或从城市级别,地区级别或国家/地区级别获取ID?

这是我的代码:

public class PopUpMenu extends JPopupMenu {

    JMenu changeLocation = null;

    public PopUpMenu() {
        changeLocation = new JMenu("Change Location");
        add(changeLocation);
        MenuActionListener listen = null;
        listen = new MenuActionListener();
        changeLocation.setAutoscrolls(true);
        for (JMenu countries : getCountry()) {
            changeLocation.add(countries);
        }
    }

    public Vector<JMenuItem> getTown(int ID) {
        Hashtable<Integer, String> v = null;
        Vector<JMenuItem> town = null;
        town = new Vector<>();
        v = new Hashtable<>();
        v.put(7, "town1");
        v.put(8, "town2");
        for (Map.Entry<Integer, String> entrySet : v.entrySet()) {
            Integer key = entrySet.getKey();
            String value = entrySet.getValue();
            JMenuItem jTown = new JMenuItem(value);
            jTown.setActionCommand("" + key + "");
            jTown.addActionListener(new MenuActionListener());
            town.add(jTown);
        }
        return town;
    }

    public Vector<JMenu> getDistrict(int ID) {
        Hashtable<Integer, String> v = null;
        Vector<JMenu> district = null;
        district = new Vector<>();
        v = new Hashtable();
        v.put(5, "district1");
        v.put(6, "district2");
        for (Map.Entry<Integer, String> entrySet : v.entrySet()) {
            Integer key = entrySet.getKey();
            String value = entrySet.getValue();
            JMenu jDistrict = new JMenu(value);
            for (JMenuItem districtes : getTown(key)) {
                jDistrict.add(districtes);

            }
            jDistrict.setActionCommand("" + key + "");
            jDistrict.addActionListener(new MenuActionListener());
            district.add(jDistrict);
        }
        return district;
    }

    public Vector<JMenu> getCity(int ID) {
        Hashtable<Integer, String> v = null;
        Vector<JMenu> city = null;
        city = new Vector();
        v = new Hashtable();
        v.put(3, "City1");
        v.put(4, "City2");
        for (Map.Entry<Integer, String> entrySet : v.entrySet()) {
            Integer key = entrySet.getKey();
            String value = entrySet.getValue();
            JMenu jCity = new JMenu(value);
            for (JMenu districties : getDistrict(key)) {
                jCity.add(districties);
            }
            city.add(jCity);
        }
        return city;
    }

    public Vector<JMenu> getCountry() {
        Hashtable<Integer, String> v = null;
        Vector<JMenu> country = null;
        country = new Vector();
        v = new Hashtable();
        v.put(1, "Country1");
        v.put(2, "Country2");
        for (Map.Entry<Integer, String> entrySet : v.entrySet()) {
            Integer key = entrySet.getKey();
            String value = entrySet.getValue();
            JMenu jCountry = new JMenu(value);
            for (JMenu cities : getCity(key)) {
                jCountry.add(cities);
            }
            country.add(jCountry);
        }
        return country;
    }
}

class MenuActionListener implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("Selected: " + e.getActionCommand());
    }
}

这是运行文件:

public class Test extends JFrame {

    public Test() {
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGap(0, 400, Short.MAX_VALUE));
        layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGap(0, 300, Short.MAX_VALUE));
        pack();
        this.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getButton() == MouseEvent.BUTTON3) {
                    doPop(e);
                }
            }

            public void doPop(MouseEvent e) {
                PopUpMenu menu = new PopUpMenu();
                menu.show(e.getComponent(), e.getX(), e.getY());
            }
        });
    }

    public static void main(String[] args) {
        new Test().setVisible(true);
    }
}

1 个答案:

答案 0 :(得分:1)

因此,要重新编写@trashgod注释和我的(感谢@trashgod),您可以在菜单上使用MouseListener.mouseCLicked并将该键作为侦听器的构造函数参数传递:

class MenuMouseListener extends MouseAdapter {

    private String id;

    public MenuMouseListener(String id) {
        this.id = id;
    }

    @Override
    public void mouseClicked(MouseEvent arg0) {
        System.out.println("Selected: " + id);
    }

}

而不是

jDistrict.setActionCommand("" + key + "");
jDistrict.addActionListener(new MenuActionListener());

你现在有

jDistrict.addMouseListener(new MenuMouseListener("" + key + ""));