首页 > 解决方案 > 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();
                    }
                });
            }
        }

此代码将显示如下形式:

在此处输入图像描述

当我按JButton4 时,输出变成这样:

在此处输入图像描述

但我需要它是这样的,所以另一个按钮会变成红色,但我按下的是我需要它变成黄色!:

在此处输入图像描述

我知道我可以使用 id 但如何为他们获取 id !或者是否有更好的方法?

标签: javaswingjbutton

解决方案


不需要 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 字符串应该与按钮的文本匹配(有一些例外)。


推荐阅读