Note that there are some explanatory texts on larger screens.

plurals
  1. POSimple Java GUI problem I cannot figure out
    text
    copied!<p>Sorry, I have asked about this before, but I have redone and simplified my code to the extent that I thought it was deserving of it's own post.</p> <p>What my code currently does : Creates 3 instances of a button class, puts them on a JPanel. Each time a button is clicked, the number of clicks is incremented and set as the text of the button. The colour of the button remains the same (except for the first click).</p> <p>What I want my code to do : After the first click, the button clicked is changed to orange, and the buttons to the right of it are changed to the next colour in the array after orange, if the button clicked is at the end of the array, start again at the beginning. Do all this in one class </p> <p>Each subsequent click should move the colours one position to the right, cycling through the array.</p> <p>What I think I need to do (after I've put waaay to much thought into this!) : Have a separate method for the first click, where a pointer indicates which button[] was clicked (i.e the source). Update all the buttons from here using a loop, finding the modulus of the number of buttons and cycling until all buttons are updated.</p> <p>After this method has ran, never run it again and cycle through the array of colours after each click.</p> <p>The problem is, I have no idea how I would implement this, despite literally days of trying. Would anyone please be able to help me with this ? I'll take any feedback or help I can get, no matter how small because this is driving me nuts ! :)</p> <p>Thank you all very much, the following is my code :</p> <pre><code>import java.awt.*; import javax.swing.*; import java.awt.event.*; 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; 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(); panel.add(buttons[i]); } frame.getContentPane( ).add( panel ); frame.setSize( 300, 300 ); frame.setVisible( true ); } private ButtonJava3(){ setBackground( Color.YELLOW ); setText( "Pick Me" ); this.addActionListener( this ); } private void updateButtons( ) { clicks++; int i = 0; do { buttons[i].setBackground( COLORS[i] ); i++; } while(i&lt;noOfButtons); setText( Integer.toString(clicks) ); } @Override public void actionPerformed( ActionEvent event ){ updateButtons( ); } } </code></pre> <p>Thank you once again, and apologies for so many questions!</p>
 

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