它是我第一次编程复选框;我想出了如何使复选框出现,并在选中时执行一个命令。但是,如果取消选中此框,而不是撤消该命令,则会再次执行此命令。如何取消选中该框时撤消该命令?取消选中复选框不会撤消命令吗?
代码:(复选框的实例)
negA = new JCheckBox("Neg");
negA.addActionListener(this);
negA.setActionCommand("A-5");
tossupA = new JCheckBox("Tossup");
tossupA.addActionListener(this);
tossupA.setActionCommand("A10");
powerA = new JCheckBox("Power");
powerA.addActionListener(this);
powerA.setActionCommand("A05");
命令:
public void actionPerformed(ActionEvent e)
{
//get the String value from the button pressed
String result = e.getActionCommand();
char team = result.charAt(0);
//set text on screen to reflect new score
String screenText = "Team "+(team)+"'s total score for this tossup: ";
//add score to subtotal and show on screentext
if(team=='A'){
teamATempScore += Integer.parseInt(result.substring(1));
screenText += teamATempScore;
}
//and now for B
else if(team=='B'){
teamBTempScore += Integer.parseInt(result.substring(1));
screenText += teamBTempScore;
}
如果没有选中该复选框,我想得分,这是递增的数量递减,而是在得分只是增量再次:(
谢谢!
(是的,如果你想知道,这是一个Quizbowl游戏的记分程序):D
好的,谢谢!但是,如果这个复选框被忽略,这会导致'else'运行吗?例如,在检查时它会使得分数增加-5(减少5),并且在检查后不检查时减少-5,但是如果从一开始就没有检查,它会减少-5吗? – Tony 2014-12-07 19:25:23
我不确定我完全理解你的问题。如果该复选框被忽略,则意味着用户从未点击过它。由于他们从不点击它,actionListener将不会听到它,并且不会执行改变分数的代码部分。 – tlukechess 2014-12-07 19:46:55
啊,我明白你的意思了。我以为你打算把它放在别的地方。问题解决了。谢谢! – Tony 2014-12-07 20:27:21