Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 尝试在单击J按钮时前后更改其颜色_Java_Swing_Colors_Boolean_Jbutton - Fatal编程技术网

Java 尝试在单击J按钮时前后更改其颜色

Java 尝试在单击J按钮时前后更改其颜色,java,swing,colors,boolean,jbutton,Java,Swing,Colors,Boolean,Jbutton,说到Java,我还是一个新手,我正在尝试使用动作监听器在每次单击按钮时更改按钮的颜色。但是我尝试了很多不同的方法,遇到了一个障碍。。。我尝试过使用布尔值,但我仍然无法解决这个问题 Color change = Color.BLACK B.setForeground(change); B.setContentAreaFilled(false); B.setFocusPainted(false); B.setBounds(100, 175, 75, 75); R.add(B); // R

说到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;
                        }

                    }

                });
在这方面

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
永远不会改变,所以它总是
黑色的
,在这种情况下,我可能会尝试做类似的事情

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有一些微妙的事情让我措手不及;)