我已经浏览过互联网,但无法找到答案:
我正在使用for循环创建36个名为a1,a2等的按钮,并同时为每个按钮分配一个唯一的Action Command。
后来我想从actionPerformed(ActionEvent e)方法中获取按钮的名称。
我可以让ActionCommand足够简单,但我也需要按钮的名称。
任何帮助非常感谢!
编辑:
以下是我正在使用的代码:
String letters[] = {"0", "a", "b", "c", "d", "e", "f"};
JButton btn[] = new JButton[35];
int count = 0;
for (int f=1; f < 7;f++){
for (int i=1; i < 7;i++){
btn[i] = new JButton(letters[f]+i, cup);
System.out.println(btn[i]));
mainGameWindow.add(btn[i]);
btn[i].addActionListener(this);
String StringCommand = Integer.toString(randomArrayNum());
btn[i].setActionCommand(StringCommand);
count++;
if(count == 18){
generateArray();
}
}
}
这为6x6网格提供了36个按钮,分别为a1-6,b1-6,c1-6等
一旦我以这种方式创建按钮,我似乎无法控制按钮,我无法分配图标或获取按钮的名称。
先谢谢。
答案 0 :(得分:10)
在Map
String letters[] = {"0", "a", "b", "c", "d", "e", "f"};
JButton btn;
int count = 0;
HashMap<String,JButton> buttonCache = new HashMap<String,JButton>();
for (int f=1; f < 7;f++){
for (int i=1; i < 7;i++){
btn = new JButton(letters[f]+i, cup);
mainGameWindow.add(btn[i]);
btn.addActionListener(this);
String stringCommand = Integer.toString(randomArrayNum());
btn.setActionCommand(stringCommand);
buttonMap.put(stringCommand,btn);
count++;
if(count == 18){
generateArray();
}
}
}
然后,在ActionListener
中,从命令中取回按钮:
public void actionPerformed(ActionEvent e) {
String command = ((JButton) e.getSource()).getActionCommand();
JButton button = buttonCache.get(command);
if (null != button) {
// do something with the button
}
}
五年后重新审视这个答案,我不知道为什么我建议HashMap
:P
此代码完全相同,没有第三方Map
:
String letters[] = {"0", "a", "b", "c", "d", "e", "f"};
int count = 0;
for (int f=1; f < 7;f++){
for (int i=1; i < 7;i++) {
String stringCommand = Integer.toString(randomArrayNum());
Button btn = new JButton(letters[f]+i, cup);
btn.setActionCommand(stringCommand);
btn.addActionListener(this);
mainGameWindow.add(btn[i]);
// NOTE : I have no idea what this is for...
count++;
if(count == 18){
generateArray();
}
}
}
<{1>} ... 中的
ActionListener
答案 1 :(得分:8)
String buttonText = ((JButton) e.getSource()).getText()
答案 2 :(得分:1)
将您的按钮存储在一个数组中,然后使用e.getSource()
找出它是...
Private JButton[] buttons;
public void actionPerformed(ActionEvent e) {
int index = -1;
for (int i = 0; i < buttons.length; i++) {
if (e.getSource() == buttons[i]) {
index = i;
break;
}
}
if (index == -1) {
// e didn't come from the buttons
} else {
// do stuff
}
}
答案 3 :(得分:1)
你有另外三个选择
1)通过工具
JButton[] buttons;
ArrayList<JButton> buttons;
但仍然需要确定从循环中按下哪个JButton
2)添加到每个JButton
单独的ActionListener
myButton.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
myButtonActionPerformed(evt);
}
private void myButtonActionPerformed(ActionEvent evt) {
// some Action
}
});
3)将javax.swing.Action添加到JButton
答案 4 :(得分:1)
您可以使用
设置图标((JButton)actionEvent.getSource()).setIcon("imageName.png");
//to get the name of button you can use
ArrayList<String> buttonNames;
buttonNames.add("button"+f+i);
答案 5 :(得分:0)
另一个可能有用的选项是使用按钮的客户端属性。基础JComponent类提供设置/获取任意属性:
Object JComponent.getClientProperty(Object key)
void JComponent.putClientProperty(Object key, Object value)
所以你可以做的是在组件上设置一个应用程序定义的属性值,然后在ActionListener方法中查询该属性值(只检查该事件的源是一个JComponent)。