Den*_*er8 2 java user-interface swing menu actionlistener
我怎样才能给出附在他们身上JMenuItem的名字ActionListener?
我有一个由单个处理的菜单系统,ActionListener这些菜单中的一些项目重复名称.这不是用户端的问题,因为它显然是做什么的; 事实上,如果他们有不同的名字会更加混乱.但是,在我的最后,我想要唯一地标记每个项目.
创建我的项目的部分如下所示:
String label = getLabel(forThisItem);
JMenuItem item = new JMenuItem(label);
item.setName(parentMenu.getName() + "_" + label);
item.addActionListener(actionListener);
parentmenu.add(item);
Run Code Online (Sandbox Code Playgroud)
使用getName()在事后(在此方法的范围之外)询问项目,给出我给它的名称,因为它应该,但输出
public void actionPerformed(ActionEvent ae) {
String actionPerformed = ae.getActionCommand();
System.out.println("actionPerformed: " + actionPerformed);
}
Run Code Online (Sandbox Code Playgroud)
是用户看到的,可能是重复的名称,由label我指定的名称,而不是我给它的唯一名称.
如何将正确的信息提供给ActionListener?
为整个JMenu实现内部ActionListener(with setActionCommand(String actionCommand))的另一种方法是为每个或实现EventHandler编写java.swing.Action(看起来对我尝试的所有内容都有效)JMenuItemListeners
关于JButtons和实现的(ActionListener以及EventHandler两个Listeners触发事件)的示例
编辑:EventHandleros太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();
}
});
}
}
Run Code Online (Sandbox Code Playgroud)