将JMenuItem的名称赋给它的ActionListener

我怎样才能给我的JMenuItem的名称附加到他们的ActionListener将看?

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

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

 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指定的label ,而不是我给出的唯一名称。

我如何将正确的信息提供给ActionListener?

你为什么不在menuitem上调用setActionCommand ? 如果您调用setActionCommand ,而不是使用setName ,则应该在调用getActionCommand时获得所需的内容

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

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

关于JButtons的例子和实现的ActionListenerEventHandler (两个Listener触发事件)

编辑: EventHandler操作系统太hacky,因为在Swing中不是另一种直接的方法如何通过string值调用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(); } }); } }