动作听众

时间:2012-04-12 02:22:57

标签: java swing actionlistener jradiobutton

以下代码是项目的动作侦听器。基本上,我有4个单选按钮,当我点击一个时,我希望它在屏幕上更改变量。当我运行代码时,它只是将所有值加在一起。还有其他方法吗?

class Calc implements ActionListener
    {
     public void actionPerformed(ActionEvent event)
     {
       double base = 0.00;
       double options;
       double total;

      if (Button25.isSelected());
      {
      base = base + 999.99;
      String base2 = Double.toString(base);
      lblBaseAns.setText(base2);
      }

      if (Button32.isSelected());
      {
        base = base + 1049.99;
        String base2 = Double.toString(base);
        lblBaseAns.setText(base2);
      }

      if (Button35.isSelected());
      {
        base = base + 1099.99;
        String base2 = Double.toString(base);
        lblBaseAns.setText(base2);
      }

      if (Button42.isSelected());
      {
        base = base + 1155.99;
        String base2 = Double.toString(base);
        lblBaseAns.setText(base2);
      }


     }
    }

1 个答案:

答案 0 :(得分:2)

问题在于,对于每个if()语句,例如 if(Button32.isSelected()); ,最后会有一个;符号。这不应该存在。这是更正后的代码......

class Calc implements ActionListener {
    public void actionPerformed(ActionEvent event){
        double base = 0.00;
        double options;
        double total;

        if (Button25.isSelected()){ // changed
            base = base + 999.99;
            String base2 = Double.toString(base);
            lblBaseAns.setText(base2);
        }
        else if (Button32.isSelected()){ // changed
            base = base + 1049.99;
            String base2 = Double.toString(base);
            lblBaseAns.setText(base2);
        }
        else if (Button35.isSelected()){ // changed
            base = base + 1099.99;
            String base2 = Double.toString(base);
            lblBaseAns.setText(base2);
        }
        else if (Button42.isSelected()){ // changed
            base = base + 1155.99;
            String base2 = Double.toString(base);
            lblBaseAns.setText(base2);
        }
    }
}

作为替代方案,为什么不从ActionEvent获取单击的按钮,然后使用if-else分支中的按钮...

class Calc implements ActionListener {
    public void actionPerformed(ActionEvent event){
        double base = 0.00;
        double options;
        double total;

        Object clickedObject = event.getSource();
        if (clickedObject instanceof JRadioButton){
            JRadioButton clickedButton = (JRadioButton)clickedObject;

            if (clickedButton == Button25){
                base = base + 999.99;
                String base2 = Double.toString(base);
                lblBaseAns.setText(base2);
            }
            else if (clickedButton == Button32){
                base = base + 1049.99;
                String base2 = Double.toString(base);
                lblBaseAns.setText(base2);
            }
            else if (clickedButton == Button35){
                base = base + 1099.99;
                String base2 = Double.toString(base);
                lblBaseAns.setText(base2);
            }
            else if (clickedButton == Button42){
                base = base + 1155.99;
                String base2 = Double.toString(base);
                lblBaseAns.setText(base2);
            }
        }
    }
}