2012-03-28 16 views
1

我正在製作一個計算器作爲Java applet。我已成功創建了佈局,並將actionListener註冊到所有JButton。爲什麼我無法比較getActionCommand()和匹配的字符串值?

所以,在我的類,它實現的ActionListener我對所有的數字按鈕的驗證碼

String buttonClicked = e.getActionCommand(); 
// for number buttons 
for(int i = 0; i <= 9; i++) 
{ 
if(i == Integer.parseInt(buttonClicked)) 
answer.setText(answer.getText() + e.getActionCommand()); 
} 

...它正常工作。那麼,爲什麼這個代碼沒有工作:

if(buttonClicked.equals("/")) 
{ 
answer.setText(answer.getText() + e.getActionCommand()); 
} 

...我們不能比較字符串與getActionCommand()字符串的內容嗎?或者我可能沒有輸入我應該的東西?

編輯 - 這是我的完整代碼:

` 
import java.awt.*; 
import javax.swing.*; 
import java.applet.*; 
import java.awt.event.*; 

public class Part_C extends Applet 
{ 
    // answer field here (no panel; JTextField is 100% width without panel) 
    private JTextField answer = new JTextField(); 
    //answer.setHorizontalAlignment(JTextField.RIGHT); // align input to the right 

    // JButton array 
    private JButton[] buttons = 
    { 
     // (panel 1) backspace, clear entry, and clear buttons here 
     new JButton("backspace"), 
     new JButton("CE"), 
     new JButton("C"), 

     // (panel 2, row 1) numbers 7 - 9, divide, and sqare-root buttons here 
     new JButton("7"), 
     new JButton("8"), 
     new JButton("9"), 
     new JButton("/"), 
     new JButton("sqrt"), 

     // (panel 2, row 2) numbers 4 - 6, multiply, and percent buttons here 
     new JButton("4"), 
     new JButton("5"), 
     new JButton("6"), 
     new JButton("X"), 
     new JButton("%"), 

     // (panel 2, row 3) numbers 1 - 3, subtract, and inverse buttons here 
     new JButton("1"), 
     new JButton("2"), 
     new JButton("3"), 
     new JButton("-"), 
     new JButton("1/x"), 

     // (panel 2, row 4) number 0, positive/negative, decimal, addition, and equals buttons here 
     new JButton("0"), 
     new JButton("+/-"), 
     new JButton("."), 
     new JButton("+"), 
     new JButton("="), 

    }; // end of JButton array 

    // constructor for class Part_C 
    public Part_C() 
    { 
     // first panel for backspace, clear entry, and clear buttons 
     JPanel panel1 = new JPanel(); 
     panel1.setLayout(new GridLayout(1, 3)); 
     for(int i = 0; i < 3; i++) 
      panel1.add(buttons[i]); 

     // second panel for operators and operands 
     JPanel panel2 = new JPanel(); 
     panel2.setLayout(new GridLayout(4, 5)); 
     for(int i = 3; i < buttons.length; i++) 
      panel2.add(buttons[i]); 

     // third panel for BorderLayout of answer JTextField, and first 2 panels 
     JPanel panel3 = new JPanel(new BorderLayout()); 
     panel3.add(answer, BorderLayout.NORTH); 
     panel3.add(panel1, BorderLayout.CENTER); 
     panel3.add(panel2, BorderLayout.SOUTH); 
     add(panel3); // add panel3 to the JFrame 

     // action listener created here 
     ButtonListener listener = new ButtonListener(); 

     // action listener is added to all JButtons here 
     for (int i = 0; i < buttons.length; i++) 
      buttons[i].addActionListener(listener); 
    } 

    /* overriding the init() method of Applet; 
    * ...in an Applet, init() is used instead 
    * of the "main" method */ 
    public void init() 
    { 
     // create JFrame 
     Part_C frame = new Part_C(); 

     // the applet stops when the window/tab is closed 
     //frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } 

    // inner class ButtonListener 
    public class ButtonListener implements ActionListener 
    { 
     public void actionPerformed(ActionEvent e) 
     { 
      String buttonClicked = e.getActionCommand(); 

      // for number buttons 
      for(int i = 0; i <= 9; i++) 
      { 
       if(i == Integer.parseInt(buttonClicked)) 
        answer.setText(answer.getText() + e.getActionCommand()); 
      } 

      // for operators 
      if(buttonClicked.equals("/")) 
      { 
       answer.setText(answer.getText() + e.getActionCommand()); 
      } 
      else if(buttonClicked.equals("sqrt")) 
      { 
       answer.setText(answer.getText() + e.getActionCommand()); 
      } 
      else if(buttonClicked.equals("X")) 
      { 
       answer.setText(answer.getText() + e.getActionCommand()); 
      } 
      else if(buttonClicked.equals("%")) 
      { 
       answer.setText(e.getActionCommand()); 
      } 
      else if(buttonClicked.equals("-")) 
      { 
       answer.setText(answer.getText() + e.getActionCommand()); 
      } 
      else if(buttonClicked.equals("1/x")) 
      { 
       answer.setText(e.getActionCommand()); 
      } 
      else if(buttonClicked.equals("+/-")) 
      { 
       answer.setText(e.getActionCommand()); 
      } 
      else if(buttonClicked.equals(".")) 
      { 
       answer.setText(answer.getText() + e.getActionCommand()); 
      } 
      else if(buttonClicked.equals("+")) 
      { 
       answer.setText(e.getActionCommand()); 
      } 

      // for data-clearing operations 
      if(buttonClicked.equals("backspace")) 
      { 
       answer.setText(answer.getText().substring(0, answer.getText().length() - 1)); 
      } 
      else if(buttonClicked.equals("CE")) 
      { 

      } 
      else if(buttonClicked.equals("C")) 
      { 
       answer.setText(""); 
      } 

      // for the equals button 
      if(buttonClicked.equals("=")) 
      { 

      } 
     } 
    } // end of inner class buttonListener 
} // end of Part_C 
+0

你真的*設置*按鈕上的actionCommand?沒有更多的代碼是我能想到的。 *編輯:拼寫:P * – mcfinnigan 2012-03-28 16:40:29

+0

Nah,沒有設置,據我所知正確使用「getActionCommand()」...我添加了上面的完整代碼: – 2012-03-28 17:06:53

+1

'..extends Applet .. // JButton array' Don'混合Swing和AWT組件。改用'JApplet'。或者更好的是,使用'JFrame'並且如果需要的話,可以使用[Java Web Start](http://stackoverflow.com/tags/java-web-start/info)從鏈接啓動它。 – 2012-03-28 17:52:57

回答

1

您正試圖做一個 「/」 一個parseInt函數。這將引發NumberFormatException。如果字符串不能轉換爲整數,你的代碼永遠不會超出for循環。

+0

不,不,我只是在JButtons上使用可以解析爲整數的字符串,按鈕0 - 9在計算器上執行parseInt ...並且該部分確實可行,但是當我嘗試比較getActionCommand( )與一個JButton有一個字符串值,不能被解析爲一個整數,它不工作(例如「/」;在任何這些情況下不使用parseInt)。 – 2012-03-28 20:16:10

+0

'for for loop'for(int i = 0; i <= 9; i ++)'無論按下什麼按鈕,都會運行EVERY TIME。在那個for循環裏面是'Integer.parseInt(buttonClicked)',它會給你一個NumberFormatException,每次你點擊一個數字以外的東西。我承諾。 – Mike 2012-03-28 20:24:08

+0

啊,對!非常感謝您指出這一點。 我在一段時間內沒有做過Java,只記得在這種情況下做異常處理,現在就開始工作。 下面是該段的代碼: String buttonClicked =「」; 嘗試 int number = Integer.parseInt(e.getActionCommand()); //爲數字按鈕 對(INT I = 0; I <= 9; i ++在) { 如果(I ==數) answer.setText(answer.getText()+ e.getActionCommand()); (NumberFormatException err) { } buttonClicked = e.getActionCommand(); } – 2012-03-28 21:08:03

相關問題