开发者

Simple Java GUI problem I cannot figure out

开发者 https://www.devze.com 2023-02-10 05:34 出处:网络
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.

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.

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).

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

Each subsequent click should move the colours one position to the right, cycling through the array.

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.

After this method has ran, never run it again and cycle through the array of colours after each click.

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 ! :)

Thank you all very much, the following is my 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<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<noOfButtons);
    setText( Integer.toString(clicks) )开发者_如何转开发;

  }
  @Override
  public void actionPerformed( ActionEvent event ){
    updateButtons( ); 
  }



}

Thank you once again, and apologies for so many questions!


import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ButtonJava3 extends JButton
  implements ActionListener{

  public static final int noOfButtons = 3;
  private static final Color[] COLORS = { Color.ORANGE,
    Color.WHITE,
    Color.GREEN};
  private int clicks;
  private static boolean firstButtonClicked = false;
  private int colorIndex, index;
  private static ButtonJava3[] buttons;

  public static void main ( String[] args ) {
    JFrame frame = new JFrame ( "ButtonJava (the Hutt)" );
    JPanel panel = new JPanel( );
    buttons = new ButtonJava3[noOfButtons];
    frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    for(int i = 0;i < noOfButtons ; i++){
        buttons[i] = new ButtonJava3(i); // One button of each color
        panel.add(buttons[i]);
    }
    frame.getContentPane( ).add( panel );
    frame.setSize( 300, 300 );
    frame.setVisible( true );
  }

  private ButtonJava3(int index){
    this.index = index;
    setBackground( Color.YELLOW );
    setText( "Pick Me" );
    this.addActionListener( this );
  }

  private void updateButtons( ) {
    clicks++;
    for(int i = 0; i< noOfButtons; i++){
        buttons[i].updateButton(); // update each button
    }
    setText( Integer.toString(clicks) );
  }
  private final void updateButton(){
    colorIndex--;; // Go to the next color
    if(colorIndex < 0){ // if there is no next color
        colorIndex = noOfButtons-1; // go back to first color
     }// apply result
    setBackground(COLORS[colorIndex]);
   }
  private final void colorOther(){
      for(int i = 0; i < noOfButtons; i++){
          if(i != index){
              buttons[i].colorIndex = checkColor(colorIndex+(i-index));
          }
      }
  }

  private final static int checkColor(int i){
      if(i >= noOfButtons){
          i -= (noOfButtons);
      }else if(i < 0){
          i = (noOfButtons)+i;
      }
      return i;
  }
  @Override
  public void actionPerformed( ActionEvent event ){
      if(!firstButtonClicked){
        colorIndex = 1;
        colorOther();firstButtonClicked = true;
      }
    updateButtons( );
  }
}


I believe this would give you what you are looking for with only a small amount of changes to your original logic:

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 < 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 < 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();
    }


}
0

精彩评论

暂无评论...
验证码 换一张
取 消