Greetings,
So far, my code compiles, but it changes white to black and then don't want to change. Supposely it should change from red->orange->green->pink->blue->black..
到目前为止,我的代码编译,但它变为白色到黑色,然后不想改变。假设它应该从红色 - >橙色 - >绿色 - >粉红色 - >蓝色 - >黑色..
public void init() {
c=new Color[] {Color.red, Color.orange, Color.green,
Color.pink, Color.blue, Color.black };
btnNext = new Button("Next Color");
btnNext.addActionListener(this);
setLayout(new BorderLayout());
add(btnNext, BorderLayout.SOUTH);
}
public void paint(Graphics g) { }
public void actionPerformed(ActionEvent e) {
if(e.getSource() == btnNext) {
for(int n=0;n<6;n++) {
setBackground(c[n]);
}
repaint();
}
}
Thank you for your help.
谢谢您的帮助。
2 个解决方案
#1
What you need to do is keep an int
member variable of the current position in the array. Then increment that position every time you click the button.
你需要做的是保持数组中当前位置的int成员变量。然后每次单击按钮时增加该位置。
// New int keeping track of background pos
private int arrPos;
public void init() {
c=new Color[] {Color.red, Color.orange, Color.green,
Color.pink, Color.blue, Color.black };
// initialize the int
arrPos = 0;
btnNext = new Button("Next Color");
btnNext.addActionListener(this);
setLayout(new BorderLayout());
add(btnNext, BorderLayout.SOUTH);
}
public void paint(Graphics g) { }
public void actionPerformed(ActionEvent e) {
if(e.getSource() == btnNext) {
// increment the background
arrPos++;
if (arrPos >= c.length) arrPos = 0;
setBackground(c[arrPos]);
repaint();
}
}
#2
What's happening is that you're looping through all of the colors all at once, every time the btnNext action is triggered. It goes by so fast that you do not see the other colors.
发生的事情是,每次触发btnNext操作时,您都会同时循环遍历所有颜色。它过得如此之快,以至于你看不到其他颜色。
What I would do is have a variable that keeps track of what position in the array you are in, and have that variable be incremented when the user hits next, and then change the background to that color. You have to get rid of the for loop inside your actionPerformed.
我要做的是有一个变量来跟踪你所在数组中的位置,并在用户点击下一个时增加该变量,然后将背景更改为该颜色。你必须摆脱actionPerformed中的for循环。
#1
What you need to do is keep an int
member variable of the current position in the array. Then increment that position every time you click the button.
你需要做的是保持数组中当前位置的int成员变量。然后每次单击按钮时增加该位置。
// New int keeping track of background pos
private int arrPos;
public void init() {
c=new Color[] {Color.red, Color.orange, Color.green,
Color.pink, Color.blue, Color.black };
// initialize the int
arrPos = 0;
btnNext = new Button("Next Color");
btnNext.addActionListener(this);
setLayout(new BorderLayout());
add(btnNext, BorderLayout.SOUTH);
}
public void paint(Graphics g) { }
public void actionPerformed(ActionEvent e) {
if(e.getSource() == btnNext) {
// increment the background
arrPos++;
if (arrPos >= c.length) arrPos = 0;
setBackground(c[arrPos]);
repaint();
}
}
#2
What's happening is that you're looping through all of the colors all at once, every time the btnNext action is triggered. It goes by so fast that you do not see the other colors.
发生的事情是,每次触发btnNext操作时,您都会同时循环遍历所有颜色。它过得如此之快,以至于你看不到其他颜色。
What I would do is have a variable that keeps track of what position in the array you are in, and have that variable be incremented when the user hits next, and then change the background to that color. You have to get rid of the for loop inside your actionPerformed.
我要做的是有一个变量来跟踪你所在数组中的位置,并在用户点击下一个时增加该变量,然后将背景更改为该颜色。你必须摆脱actionPerformed中的for循环。