Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I believe this would give you what you are looking for with only a small amount of changes to your original logic: </p> <pre><code>public class ButtonJava3 extends JButton implements ActionListener { public static int noOfButtons = 3; private static final Color[] COLORS = {Color.ORANGE, Color.WHITE, Color.GREEN}; private int clicks; private static ButtonJava3[] buttons; private static ButtonJava3 first; private int myIndex; private int colorIndex = -1; public static void main(String[] args) { JFrame frame = new JFrame("ButtonJava (the Hutt)"); JPanel panel = new JPanel(); buttons = new ButtonJava3[3]; frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); for (int i = 0; i &lt; buttons.length; i++) { buttons[i] = new ButtonJava3(i); panel.add(buttons[i]); } frame.getContentPane().add(panel); frame.setSize(300, 300); frame.setVisible(true); } private ButtonJava3(int myIndex) { this.myIndex = myIndex; setBackground(Color.YELLOW); setText("Pick Me"); this.addActionListener(this); } private void incrementColors() { colorIndex++; for (int i = 0; i &lt; noOfButtons; i++) { int j = myIndex + i; System.out.println((j%noOfButtons) +":"+((colorIndex + i) % noOfButtons)); buttons[j % noOfButtons].setBackground(COLORS[(colorIndex + i) % noOfButtons]); buttons[j % noOfButtons].setOpaque(true); } } private void updateButtons() { if (first == null) { first = this; } first.incrementColors(); setText(Integer.toString(++clicks)); } public void actionPerformed(ActionEvent event) { updateButtons(); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload