取消选中复选框不撤消命令?

时间:2014-12-07 18:57:48

标签: java checkbox

这是我第一次编程复选框;我想出了如何显示一个复选框,并在选中时执行命令。但是,如果取消选中该框,则不会撤消该命令,而是再次执行该命令。取消选中该框时如何撤消命令?

代码:(复选框的实例化)

    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

2 个答案:

答案 0 :(得分:1)

监听器只是检查是否单击了复选框 - 它不会检查它是否从未选中检查到检查,反之亦然。

使用.isSelected()方法确定单击复选框后是否选中该复选框。

例如:

 if (negA.isSelected()) 
 {
      //the checkbox was checked after they clicked it, do something
 }
 else 
 {
      //the checkbox was unchecked after they clicked it, do something else
 } 

答案 1 :(得分:0)

您必须检查控件的状态,然后递增或递减。编译器无法自动生成代码的反转。

要检查复选框的状态,请在其上调用isSelected()方法,如下例所示。

import javax.swing.JOptionPane;

public class CheckboxExample extends javax.swing.JFrame {

    private javax.swing.JCheckBox jCheckBox1;

    public CheckboxExample() {
        jCheckBox1 = new javax.swing.JCheckBox();
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new java.awt.FlowLayout());

        jCheckBox1.setText("CheckMe");
        jCheckBox1.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBox1ActionPerformed(evt);
            }
        });
        getContentPane().add(jCheckBox1);

        pack();
    }

    // this will get called when the state of the checkbox changes
    private void jCheckBox1ActionPerformed(java.awt.event.ActionEvent evt) {
        if(jCheckBox1.isSelected()) {
            JOptionPane.showMessageDialog(this, "Checked", "Message", 
                                          JOptionPane.INFORMATION_MESSAGE);
        }
        else {
            JOptionPane.showMessageDialog(this, "Unchecked", "Message", 
                                          JOptionPane.INFORMATION_MESSAGE); 
        }            
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new CheckboxExample().setVisible(true);
            }
        });
    }
}