java - java swing获取或设置新JButton的唯一ID,同时在for循环上创建它
问题描述
我有创建的 java swing 类JButton
它正在工作,但我需要的是当我按下JButton
让种子它是数字 1 时,代码中的代码ActionEvent
是更改背景,JButton
但我需要的是,如果我按下另一个JButton
,我需要它回到红色颜色 :
例子 :
package Classes;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class testbtn {
public JFrame frame = new JFrame();
public int copcounter = 5;
public testbtn() {
JPanel jdb = new JPanel();
jdb.setLayout(new FlowLayout());
for (int x = 1; x <= copcounter; x++) {
JButton btn = new JButton();
btn.setText(String.valueOf(x));
if (x == 1) {
btn.setBackground(Color.yellow);
} else {
btn.setBackground(Color.red);
}
btn.putClientProperty("id", x);
btn.addActionListener((ActionEvent e) -> {
btn.setBackground(Color.yellow);
System.out.println(e.getID());
});
jdb.add(btn);
}
frame.add(jdb);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new testbtn();
}
});
}
}
此代码将显示如下形式:
当我按JButton
4 时,输出变成这样:
但我需要它是这样的,所以另一个按钮会变成红色,但我按下的是我需要它变成黄色!:
我知道我可以使用 id 但如何为他们获取 id !或者是否有更好的方法?
解决方案
不需要 id,只需将所有 JButtons 放入一个List<JButton>
,比如说称为 called buttonList
,然后遍历按钮的 ActionListener 中的列表,将列表中所有按钮的背景变为红色,然后将当前按钮的背景变为黄色。
然后在使用它的代码中:
public void actionPerformed(ActionEvent e) {
// iterate through the list
for (JButton button : buttonList) {
button.setBackground(Color.RED);
}
// then set *this* button's color yellow:
((JButton) e.getSource).setBackground(Color.YELLOW);
}
而已
或为您的代码...
public class TestBtn {
public JFrame frame = new JFrame();
public int copcounter = 5;
private List<JButton> buttonList = new ArrayList<>();
public TestBtn() {
JPanel jdb = new JPanel();
jdb.setLayout(new FlowLayout());
for (int x = 1; x <= copcounter; x++) {
JButton btn = new JButton();
// add this
buttonList.add(btn);
btn.setText(String.valueOf(x));
if (x == 1) {
btn.setBackground(Color.yellow);
} else {
btn.setBackground(Color.red);
}
// btn.putClientProperty("id", x);
btn.addActionListener((ActionEvent e) -> {
// iterate through the list
for (JButton button : buttonList) {
button.setBackground(Color.RED);
}
// then set *this* button's color yellow:
((JButton) e.getSource).setBackground(Color.YELLOW);
// show button text
System.out.println(e.getActionCommand());
});
jdb.add(btn);
}
frame.add(jdb);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new testbtn();
}
});
}
}
此外,ActionEvent 的 actionCommand 字符串应该与按钮的文本匹配(有一些例外)。
推荐阅读
- amazon-web-services - 是否可以为多个 aws ec2 实例创建共享 RAM 池?
- node.js - 服务器和客户端应该如何交互
- cryptography - sustes 命令和 wc.conf 加密挖掘黑客
- python - django 在用户创建时创建行
- forms - 当表单打开时,我需要帮助计算两个 Access 表单文本框
- javascript - 这个带有匿名闭包的 React 代码对 validateFormat 有什么作用?
- angular - 由于错误,预渲染失败:ReferenceError:未定义窗口?
- c# - Chrome 驱动程序 - 即使使用 --no-options 60 秒后仍然超时
- php - 在循环中使用 number_format
- matlab - 将 findspeaks 函数从列旋转到行