问题是我创建了一个弹出菜单供人们选择他来自何处,如下所示:
更改位置->国家->城市->区域->城镇。
在最后一个城镇中,它是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);
    }
}

最佳答案

因此,要改写@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 + ""));

10-08 01:59