我无法弄清楚简单的 Java GUI 问题
抱歉,我之前曾问过这个问题,但我已经重做并简化了我的代码,以至于我认为它值得拥有自己的帖子。
我的代码当前的作用:创建按钮类的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信这只需对原始逻辑进行少量更改即可为您提供所需的内容:
I believe this would give you what you are looking for with only a small amount of changes to your original logic: