从 ActionListener 获取按钮名称?
我已经在互联网上搜索过,但找不到答案:
我使用 for 循环创建 36 个名为 a1、a2 等的按钮,并同时为每个按钮分配一个唯一的操作命令。
后来我想从 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 等,
一旦创建按钮,我似乎无法控制它们这样,我无法分配图标或获取按钮的名称。
提前致谢。
I have scoured the internet but can't find an answer to this :
I'm using a for loop to create 36 buttons called a1, a2, etc. and assigning each of them a unique Action Command at the same time.
Later on I wanted to get the name of the button from the actionPerformed(ActionEvent e) method.
I could get the ActionCommand easy enough, but I need the name of the button as well.
Any help much appreciated!
Edit:
Here is the code I'm using:
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();
}
}
}
This gives you 36 buttons for a 6x6 grid that go a1-6, b1-6, c1-6 etc
I just can't seem to control the buttons once I've created them this way, I can't assign icons or get the name of the button.
Thanks in Advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
在
Map
中保留按钮的引用然后,在您的
ActionListener
中,从命令中获取按钮:编辑
五年后重新访问此答案,我不知道为什么我建议使用
HashMap
:P此代码执行完全相同的操作,没有第三方
Map
:在
ActionListener
中...Keep a reference of the buttons in a
Map
Then, in your
ActionListener
, get the button back from the command :Edit
Revisiting this answer over five years later, I have no idea why I suggested a
HashMap
:PThis code does the exact same thing, no third party
Map
:in the
ActionListener
...将按钮存储在数组中并使用
e.getSource()
找出它是哪个...Store your buttons in an array and use
e.getSource()
to figure out which it was...您还有另外三个选择
1) 通过实现
JButton[] 按钮;
ArrayList;按钮;
但仍然需要确定从循环中按下哪个
JButton
2) 向每个
JButton
添加单独的ActionListener
3)将 javax.swing.Action 添加到
JButton< /代码>
you have another three choices
1) by implements
JButton[] buttons;
ArrayList<JButton> buttons;
but still is required to determine which
JButton
is pressed from the loop2) add to each
JButton
separateActionListener
3) add javax.swing.Action to the
JButton
您可以使用设置图标
you can set the Icon by using
另一个可能有效的选项是使用按钮的客户端属性。 JComponent 基类提供设置/获取任意属性的功能:
因此您可以做的是在组件上设置应用程序定义的属性值,然后在 ActionListener 方法内询问该属性值(只需检查事件源是否为 JComponent)。
Another option that might work is to use the client properties of the button. The base JComponent class provides for setting/getting arbitrary properties:
So what you can do is set an application defined property value on the component and then interrogate that property value inside the ActionListener method (just checking that source of the event is a JComponent).