簡體   English   中英

我可以從JMenu(不是JMenuItem)獲取ActionCommand嗎?

[英]Can I getActionCommand from JMenu (not JMenuItem)?

問題是我創建了一個彈出菜單,供人們選擇他的來源,例如:更改位置->國家->城市->區域->城鎮。 在最后一個城鎮中,它是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 + ""));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM