Adding a random color button

random color buttons

To add another button you need to do 4 things:

  1. create another button , this button must have a different name from any other buttons on this panel
  2. add the new button to the panel
  3. connect the button to a listener, the name of this listener must be different from others listeners in the program.
  4. Write the code for the listener
    1. the listener is a new class inside of the Panel class that implements ActionListener
    2. This listener must have a different class name than any other listener in your program, for example you could call it ColorListener
    3. write code that says:
      1. pick a random number and assign it to a variable
      2. if the variable is less than 0.333 set the background color to your first color choice
      3. else if the variable is less than 0.6666 set the background color to your second color choice
      4. 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);