2012-04-12 101 views
1

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

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); 
     } 


    } 
    } 
+0

”*将所有值加在一起*“。怎么样? – Lion 2012-04-12 02:27:58

+0

我不知道为什么,但是当我做一个触发代码的动作时,它会将每个单选按钮的所有值加在一起并显示出来。 – Brandon 2012-04-12 02:29:08

+0

这不应该发生。你可能错过了**组**。 – Lion 2012-04-12 02:31:07

回答

2

的问题是,对于每个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); 
      } 
     } 
    } 
} 
+0

我完全没有注意到;错误。只要我解决了这个问题,它就解决了这个问题。谢谢! – Brandon 2012-04-12 02:49:56

+0

我没有注意到它,直到我去写我的代码建议。 – wattostudios 2012-04-12 02:51:04