【问题标题】:Trying to change the color of a J Button back and forth when it is clicked尝试在单击时来回更改 J 按钮的颜色
【发布时间】:2020-03-19 02:31:03
【问题描述】:

当谈到 Java 时,我仍然是一个新手,我试图通过使用动作侦听器在每次单击按钮时更改按钮的颜色。但是我尝试了许多不同的方法并且遇到了障碍......我尝试使用布尔值,但我仍然无法解决这个问题。

Color change = Color.BLACK    
B.setForeground(change);
B.setContentAreaFilled(false);
B.setFocusPainted(false);
B.setBounds(100, 175, 75, 75);

R.add(B); // R is the JFrame the button is added to...

             B.addActionListener(new ActionListener() { //Action Listener when pressing should change the color from Black to Red
                        public void actionPerformed(ActionEvent e) {
                            boolean right = false;
                            if (change == Color.BLACK) {
                                right = true;
                                B.setForeground(Color.red);
                            }
                        if (right == true) {
                            B.setForeground(Color.BLACK);
                            right = false;
                        }

                    }

                });

【问题讨论】:

    标签: java swing colors boolean jbutton


    【解决方案1】:

    在这种情况下...

    B.addActionListener(new ActionListener() { //Action Listener when pressing should change the color from Black to Red
        public void actionPerformed(ActionEvent e) {
            boolean right = false;
            if (change == Color.BLACK) {
                right = true;
                B.setForeground(Color.red);
            }
            if (right == true) {
                B.setForeground(Color.BLACK);
                right = false;
            }
        }
    });
    

    right 将始终为 false,因为它是在 actionPerformed 方法中本地声明的。

    改为创建ActionListener 的实例字段,例如...

    B.addActionListener(new ActionListener() { //Action Listener when pressing should change the color from Black to Red
        private boolean right = false;
        public void actionPerformed(ActionEvent e) {
            if (change == Color.BLACK) {
                right = true;
                B.setForeground(Color.red);
            }
            if (right == true) {
                B.setForeground(Color.BLACK);
                right = false;
            }
    
        }
    
    });
    

    另外,change 永远不会改变,所以它总是BLACK,在这种情况下,我可能会想要做更多这样的事情......

    B.addActionListener(new ActionListener() { //Action Listener when pressing should change the color from Black to Red
        private boolean right = false;
        public void actionPerformed(ActionEvent e) {
            if (!right) {
                B.setForeground(Color.red);
            } else if (right) {
                B.setForeground(Color.BLACK);
            }
            right = !right;
        }
    
    });
    

    【讨论】:

    • 哦,哇,我应该看到那个错误了,感谢您花时间帮助像我这样的初学者,我感谢像您这样的人!再次感谢。
    • @JoshuaSaikali 有些微妙的事情让我措手不及;)
    猜你喜欢
    • 1970-01-01
    • 2021-07-07
    • 2013-04-12
    • 2012-12-06
    • 2013-06-05
    • 2016-09-08
    • 1970-01-01
    • 2021-05-21
    • 1970-01-01
    相关资源
    最近更新 更多