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

发布于 2024-10-16 23:43:07 字数 1806 浏览 5 评论 0原文

抱歉,我之前曾问过这个问题,但我已经重做并简化了我的代码,以至于我认为它值得拥有自己的帖子。

我的代码当前的作用:创建按钮类的 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( ); 
  }



}

再次感谢大家,并对这么多问题表示歉意!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我纯我任性 2024-10-23 23:43:07
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( );
  }
}
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( );
  }
}
扭转时空 2024-10-23 23:43:07

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

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


}

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


}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文