Write a program that has only one button in the frame. Clicking on the button cycles through the colors: red --> green --> blue --> gray --> red --> and so on, one color change per click. In addition to the setBackground( Color.color ) method we have been using, you will need the Color getBackground() method to get the current color.

Another way to do this (a somewhat cruder way) is to use an instance variable (of type Color) in the frame to hold the current color.

Have an array of Color instances with the potential colors and a global index.
With click of the button use the element at the current index and increase the index by one. So with the next click the next color will be used.
When the index reaches the length of the array set it to zero in order to start from the beginning:

int index = 0;
Color [] array = {......};

......
onButtonClick() {
  use Color at array[index];
  index++;
  if (index==array.length) index=0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.