简单的Java GUI问题我无法弄清楚

时间:2011-02-12 00:53:35

标签: java user-interface jframe jpanel jbutton

很抱歉,我之前已经问过这个问题,但是我已经重做并简化了我的代码,以至于我认为它应该得到它自己的帖子。

我的代码目前的作用:创建一个按钮类的3个实例,将它们放在JPanel上。每次单击按钮时,点击次数都会增加并设置为按钮的文本。按钮的颜色保持不变(第一次点击除外)。

我希望我的代码能做什么:第一次点击后,点击的按钮变为橙色,如果点击的按钮位于橙色,则橙色后按钮右侧的按钮将更改为数组中的下一个颜色数组的结尾,在开头再次开始。在一个班级做所有这些

每次后续点击都应将颜色向右移动一个位置,然后在数组中循环。

我认为我需要做的事情(在我对此进行深思熟虑之后!):为第一次点击设置一个单独的方法,其中一个指针指示单击了哪个按钮[](即源)。使用循环从这里更新所有按钮,找到按钮数量的模数并循环直到所有按钮都更新。

运行此方法后,再也不要再运行它,并在每次单击后循环显示颜色数组。

问题是,我不知道如何实现这一点,尽管几天都在尝试。有人可以帮我这个吗?我会接受任何反馈或帮助,无论多小,因为这让我疯了! :)

非常感谢,以下是我的代码:

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



}

再次感谢你,并为这么多问题道歉!

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:2)

我相信只要对原始逻辑进行少量更改,这就可以为您提供所需的内容:

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


}