【问题标题】:Giving JMenuItem's name to it's ActionListener将 JMenuItem 的名称赋予它的 ActionListener
【发布时间】:2012-02-18 21:43:49
【问题描述】:

我怎样才能给我的JMenuItems 命名,以便ActionListener 附加到他们身上?

我有一个由单个 ActionListener 处理的菜单系统,并且这些菜单中的某些项目名称重复。这在用户端不是问题,因为很明显什么做什么;事实上,如果他们有不同的名字会更加混乱。但是,在我的最后,我想为每个项目单独标记。

创建我的项目的部分如下所示:

String label = getLabel(forThisItem);
JMenuItem item = new JMenuItem(label);
item.setName(parentMenu.getName() + "_" + label);
item.addActionListener(actionListener);
parentmenu.add(item);

之后使用 getName() 询问项目(并且在此方法的范围之外)给出了我给它的名称,这是应该的,但输出是

public void actionPerformed(ActionEvent ae) {
    String actionPerformed = ae.getActionCommand();
    System.out.println("actionPerformed: " + actionPerformed);
}

是用户看到的可能重复的名称,由label 指定,而不是我给它的唯一名称。

如何向 ActionListener 提供正确的信息?

【问题讨论】:

    标签: java swing user-interface menu actionlistener


    【解决方案1】:

    另一种为整个JMenu 实现内部ActionListener(与setActionCommand(String actionCommand))的方法是为每个JMenuItem 编写java.swing.Action 或实现EventHandler(似乎对所有Listeners 都有效,我试过了)

    关于 JButtons 的示例以及已实现的 ActionListener 和 EventHandler(Listeners 触发事件)

    编辑:EventHandler os 太老套了,因为在 Swing 中不是另一种直接方法,如何通过字符串值调用 code_block

    import java.awt.EventQueue;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.beans.EventHandler;
    import java.util.ArrayList;
    import java.util.List;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    /** based on  @see http://stackoverflow.com/questions/7702697 */
    public class GridButtonPanel extends JPanel {
    
        private static final int N = 2;
        private final List<GridButton> list = new ArrayList<GridButton>();
    
        public GridButtonPanel() {
            super(new GridLayout(N, N));
            for (int i = 0; i < N * N; i++) {
                int row = i / N;
                int col = i % N;
                GridButton gb = new GridButton(row, col);
                gb.addActionListener((ActionListener) EventHandler.create(ActionListener.class, this,
                        "actionName" + row + "A" + col));
                list.add(gb);
                this.add(gb);
            }
        }
    
        public void actionName0A0() {
            System.out.println(" Grid at Row 0, Column 0 ");
        }
    
        public void actionName0A1() {
            System.out.println(" Grid at Row 0, Column 1 ");
        }
    
        public void actionName1A0() {
            System.out.println(" Grid at Row 1, Column 0 ");
        }
    
        public void actionName1A1() {
            System.out.println(" Grid at Row 1, Column 1 ");
        }
    
        private GridButton getGridButton(int r, int c) {
            int index = r * N + c;
            return list.get(index);
        }
    
        private class GridButton extends JButton {
    
            private int row;
            private int col;
    
            public GridButton(int row, int col) {
                super("Row - " + row + ",  Col - " + col);
                this.row = row;
                this.col = col;
                this.addActionListener(new ActionListener() {
    
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        int r = GridButton.this.row;
                        int c = GridButton.this.col;
                        GridButton gb = GridButtonPanel.this.getGridButton(r, c);
                        System.out.println("r" + r + ",c" + c
                                + " " + (GridButton.this == gb)
                                + " " + (GridButton.this.equals(gb)));
                    }
                });
            }
        }
    
        private void display() {
            JFrame f = new JFrame("GridButton");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(this);
            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    new GridButtonPanel().display();
                }
            });
        }
    }
    

    【讨论】:

      【解决方案2】:

      您为什么不在菜单项上调用setActionCommand。如果你调用setActionCommand,而不是使用setName,你应该得到你调用getActionCommand时所期望的结果

      另外,它的标签,不是标签。

      【讨论】:

        猜你喜欢
        • 2012-02-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多