0

I am trying to create this simple GUI where the number of clicks is displayed on the button and incremented after each click, and so that after each click, the colours of each button are rotated one value to the right. At the moment, the GUI is created, but the background is not set and nothing happens when you click on anything. I can't seem to find a problem here. Can anyone see any ?

Thanks a lot for your help with this :)

 import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;


public class ButtonJava extends JButton implements ActionListener  {
  private static int currentColor=0;
  private int clicks;
  private static final Color[] COLORS = {
    Color.ORANGE,
    Color.WHITE,
    Color.GREEN };

  public ButtonJava( ){
    setBackground( Color.YELLOW );
    setText( "Pick ME" );
    this.addActionListener( this );
  }

  public static void main(String[] args) {
    JFrame frame = new JFrame ("JFrame");
    JPanel panel = new JPanel( );
    frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
    JButton buttons[] = new JButton[3];
    for(int i = 0;i<buttons.length ; i++){
      buttons[i] = new ButtonJava(); 
      panel.add(buttons[i]);
    }
    frame.getContentPane( ).add( panel );
    frame.setSize( 500, 500);
    frame.setVisible( true );
  }

  private void updateButton() {
    changeColors();
    clicks++;
    setText( "# of clicks = " + Integer.toString(clicks) );
  }

  private void changeColors( ) {
    for (int i=0;i<COLORS.length;i++){
      setBackground(COLORS[currentColor]);
      currentColor %=2;
    }
  }

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


}
4
  • Ah, I have rectified my initial mistake now, but I have implemented the call to changeColors() incorrectly, it is only changing the color of one button rather than all three. Can anyone help please ? Commented Feb 8, 2011 at 20:50
  • 1
    @user476033: Each ButtonJava object only has control of itself, which means when you click on one of the buttons, only that button will change color. If you want to click on one of the buttons and have ALL of the buttons change color, you're going to have to provide the other buttons to each button's constructor, and then have changeColor call setBackground on each of the other buttons. Commented Feb 8, 2011 at 21:10
  • @shakedown: When you say "you're going to have to provide the other buttons to each button's constructor", what do you mean by this ? How would I go about doing this ? Thank you very much for your help Commented Feb 8, 2011 at 21:15
  • @user476033: You need a way to reference the other buttons from inside your ButtonJava. One way of doing this is to have your ButtonJava constructor take a list of JButton (or ButtonJava), and then in changeColor you would iterate over that list of buttons and call setBackground on each one. Commented Feb 8, 2011 at 21:34

3 Answers 3

1

Simple mistake - you're not creating your custom button class, you're using JButton.
Change the following line:
buttons[i] = new JButton("Pick Me");
To:
buttons[i] = new ButtonJava();

Sign up to request clarification or add additional context in comments.

Comments

0

In main, you're making normal JButtons and adding them to your UI when I guess you mean to be adding ButtonJava instead.

Comments

0

Shouldn't it be :

ButtonJava buttons[] = new ButtonJava[3];
for(int i = 0;i<buttons.length ; i++){
    buttons[i] = new ButtonJava(); 
    panel.add(buttons[i]);
 }

?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.