JAVA:jButton单击时设置背景更改事件

JAVA:jButton单击时设置背景更改事件,java,swing,toggle,jbutton,Java,Swing,Toggle,Jbutton,我一直在到处寻找我的问题的解决方案,但没有找到任何有效的方法: 要求:在红色和绿色背景颜色之间切换按钮“颜色!” 状态:当我第一次单击按钮时,它会变为红色,下次单击时不会变为绿色 这是我目前掌握的代码: private void jButton1MouseClicked(java.awt.event.MouseEvent evt) { Color colors[] = new Color[] { Color

我一直在到处寻找我的问题的解决方案,但没有找到任何有效的方法: 要求:在红色和绿色背景颜色之间切换按钮“颜色!”

状态:当我第一次单击按钮时,它会变为红色,下次单击时不会变为绿色

这是我目前掌握的代码:

private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
         Color colors[] = new Color[]
                {
                    Color.red, Color.green
                };
       for (int i = 0; i <= (colors.length-1); i++)
       {
        jButton1.setBackground(colors[i]);            
        }  

使用带有按钮的
ActionListener
而不是
MouseListener
,鼠标不是触发按钮的唯一方式

您需要一些方法来了解按钮的当前状态,例如,您可以

  • 检查
    if
    语句中按钮的当前颜色,然后切换到其他颜色
  • 使用
    布尔值
    在状态之间切换
  • 使用模块化数学(我知道)
例如

public class TestPane extends JPanel {

    private int clickCount = 0;

    public TestPane() {
        JButton btn = new JButton("Click");
        btn.setContentAreaFilled(false);
        btn.setBackground(Color.RED);
        btn.setOpaque(true);
        btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                clickCount++;
                if (clickCount % 2 == 0) {
                    System.out.println("Red");
                    btn.setBackground(Color.RED);
                } else {
                    System.out.println("Green");
                    btn.setBackground(Color.GREEN);
                }
            }
        });
        add(btn);
    }

}
该按钮以(空)开头,因此第一次单击应变为红色,第二次单击应变为绿色,第三次单击应变为红色,等等

如果您有两种以上的颜色,那么您可以简单地使用

if (clickCount > 0 && clickCount < COLORS.length) {
    btn.setBackground(COLORS[clickCount]);
}
if(clickCount>0&&clickCount

首先,不要在按钮上使用
MouseListener
,而要使用
ActionListener
,其次,如何确定按钮的当前状态,以便知道应该更改为哪个状态?因此,在设置颜色之前,我需要
getBackground
?在循环之前或循环内部添加
getBackground
,将按钮更改为绿色。不需要循环。。。有点您需要能够在数组中找到当前颜色的索引,然后确定下一步要使用的索引…如果@Edgar想要有许多颜色,例如颜色[],那么他可以将if case替换为(clickCount%colors.length)@SrinathGanesh或可能使用
列表
,但两者都可以工作;)该按钮启动一个
(null)
,因此第一次单击应变为红色,第二次单击应变为绿色,第三次单击应变为红色,等等。@SrinathGanesh我尝试了此操作,但没有成功;始终保持红色。好的,所以使用
btn.setBackground(null)而不是
btn.setBackground(颜色为红色)
并将
if
语句更改为仅与
clickCount
值一起使用,因此当它是
1
时,它是红色的,当它是
2
时,它是绿色的,其他任何内容都将被忽略
jButton1.setBackground(Color.black);

private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
             Color colors[] = new Color[]
                    {
                        Color.red, Color.green
                    };    
    if (jButton1.getBackground() == Color.black || jButton1.getBackground() == Color.green)
               {
                   jButton1.setBackground(colors[0]);
               }
               else
               {
                   jButton1.setBackground(colors[1]);
               } }
if (clickCount > 0 && clickCount < COLORS.length) {
    btn.setBackground(COLORS[clickCount]);
}
jButton1.setBackground(Color.black);

private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
             Color colors[] = new Color[]
                    {
                        Color.red, Color.green
                    };    
    if (jButton1.getBackground() == Color.black || jButton1.getBackground() == Color.green)
               {
                   jButton1.setBackground(colors[0]);
               }
               else
               {
                   jButton1.setBackground(colors[1]);
               } }