我试图在一个方法中从颜色中更改4个按钮,但是即使我按下其中一个按钮也会继续计数器,我希望每个按钮都为自己计数并从0开始而不是通过其他按钮已停止。
int count = 0;
public void ColourChange(JButton jButtonColour1){
//Changes the colours of the buttons, doesn't work good because it goes on counting for the other buttons too.
//returns the counter to 0 after it gets to 10.
if (count >= 10) {
count = 0;
}
count++;
//Makes sure it can be used at all the 4 buttons instead on just 1.
JButton setColour = jButtonColour1, jButtonColour2, jButtonColour3, jButtonColour4;
Color color;
switch (count) {
case 1:
count = 1;
setColour.setBackground(Color.BLACK);
break;
case 2:
count = 2;
setColour.setBackground(Color.RED);
break;
case 3:
count = 3;
setColour.setBackground(Color.PINK);
break;
case 4:
count = 4;
setColour.setBackground(Color.GREEN);
break;
case 5:
count = 5;
setColour.setBackground(Color.BLUE);
break;
case 6:
count = 6;
setColour.setBackground(Color.YELLOW);
break;
case 7:
count = 7;
setColour.setBackground(Color.MAGENTA);
break;
case 8:
count = 8;
setColour.setBackground(Color.cyan);
break;
case 9:
count = 9;
setColour.setBackground(Color.GRAY);
break;
case 10:
count = 10;
setColour.setBackground(Color.ORANGE);
break;
}
}
答案 0 :(得分:0)
string buttonName="";
public int ColourChange(JButton jButtonColour1,int count){
//some codes
if(!(buttonName.equals(jButtonColour1.getText()))){
buttonName=jButtonColour1.getText();//name or what are u using
count = 0;
}
//some codes
switch (count) {
case 1:
count = 1;
setColour.setBackground(Color.BLACK);
break;
case 2:
count = 2;
setColour.setBackground(Color.RED);
break;
//............
}