简体   繁体   English

JAVA:jButton setBackground在单击事件时发生更改

[英]JAVA: jButton setBackground change on click event

I've been looking everywhere for a solution to my issue but haven't found anything that works: REQUIREMENT: toggle between RED & GREEN background colours for jButton 'Colour!' 我一直在到处寻找解决问题的方法,但没有找到任何可行的方法:要求:在jButton'Colour!'的红色和绿色背景颜色之间切换。

STATUS: When I click the button the first time, it changes to RED and does not change to GREEN on next click. 状态:当我第一次单击该按钮时,它变为红色,并且在下次单击时不变为绿色。

This is the code I have so far: 这是我到目前为止的代码:

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]);            
        }  

UPDATE (solution): 更新(解决方案):

 if (jButton1.getBackground() == Color.black || jButton1.getBackground() == Color.green)
       {
           jButton1.setBackground(colors[0]);
       }
       else
       {
           jButton1.setBackground(colors[1]);
       } 

Use an ActionListener instead of a MouseListener with buttons, a mouse isn't the only way a button can be triggered. 使用ActionListener而不是带有按钮的MouseListener ,鼠标不是触发按钮的唯一方法。

You need some way to know the current state of the button, for example, you could... 您需要某种方式来了解按钮的当前状态,例如,您可以...

  • check the current color of the button in a if statement and switch to the other color if语句中检查按钮的当前颜色,然后切换到其他颜色
  • use a boolean value to switch between states 使用boolean值在状态之间切换
  • use modular maths (I know scary) 使用模块化数学(我知道这很可怕)

For example... 例如...

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);
    }

}

The button starts off a (null) so the first click should change to RED, second to GREEN, third to RED, etc... 该按钮以(null)开始,因此第一次单击应更改为RED,第二次更改为GREEN,第三次更改为RED,依此类推...

public class TestPane extends JPanel {

    protected static final Color[] COLORS = new Color[]{null, Color.RED, Color.GREEN};
    private int clickCount = 0;

    public TestPane() {
        JButton btn = new JButton("Click");
        btn.setContentAreaFilled(false);
        btn.setBackground(null);
        btn.setOpaque(true);
        btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                clickCount++;
                switch (clickCount) {
                    case 1:
                    case 2:
                        btn.setBackground(COLORS[clickCount]);
                        break;
                }
            }
        });
        add(btn);
    }

}

If you have more then two colors, then you could simply use 如果您有两种以上的颜色,则可以简单地使用

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

instead of the switch statement 而不是switch语句

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]);
               } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM