samedi 8 octobre 2022

If Statement only lets me change content pane background color by clicking a menu button once. How do I make it change every time I click?

I'm learning Java and am loving experimenting to learn new things; however, I am stuck on what I believe is a logic flaw with my program. What I want to do is have the background of my content pane change to a random color every time I click the menu item "Change Color". This works on the initial click but not on any following clicks. I have to restart the program for it to work again. Here is what I have so far...

Global:

Random rand = new Random();

float r = rand.nextFloat();
float g = rand.nextFloat();
float b = rand.nextFloat();

Color randomColor = new Color(r,g,b);

Main():

getContentPane().setBackground(Color.WHITE);

Action Listener:

if(event.getSource().equals(i3)) { //Change Color
        getContentPane().setBackground(randomColor);
    }

i3 is my menu item "Change Color"

Thanks in advance!




Aucun commentaire:

Enregistrer un commentaire