Adding a random color button
To add another button you need to do 4 things:
- create another button , this button must have a different name from any other buttons on this panel
- add the new button to the panel
- connect the button to a listener, the name of this listener must be different from others listeners in the program.
- Write the code for the listener
- the listener is a new class inside of the Panel class that implements ActionListener
- This listener must have a different class name than any other listener in your program, for example you could call it ColorListener
- write code that says:
- pick a random number and assign it to a variable
- if the variable is less than 0.333 set the background color to your first color choice
- else if the variable is less than 0.6666 set the background color to your second color choice
- else set the label color to your third color choice
To set the color of the background in the Listener, use this code:
setBackground(Color.YELLOW);