简体   繁体   中英

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!'

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.

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
  • use a boolean value to switch between states
  • 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...

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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