2013-07-28 40 views
1

我是新來的Java,並設置自己的任務嘗試創建一個簡單的計算器(和GUI),以提高我的理解和語言的技能。計算器問題與簡單的邏輯

藉此代碼:

import java.awt.FlowLayout; 
import java.io.*; 
import java.awt.*; 
import java.awt.event.*; 

import javax.swing.*; 

import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 


public class calc extends JFrame { 


    public JTextField input; 
    public JTextField output; 
    public JPanel Window; 
    public JButton math_button[] = new JButton[5]; 
    public JButton number_button[] = new JButton[10]; 
    public String[] number_button_name = {"1","2","3","4","5","6","7","8","9","0"}; 
    public String[] name = {"Add", "Mulitply", "Divide", "Subtract", "Equals"}; 
    public JFrame frame = new JFrame(); 
    public JPanel math_panel = new JPanel(); 
    public JPanel number_panel = new JPanel(); 
    public JTextField math_input = new JTextField(); 
    boolean trrgger = false; 

    thehandler handler = new thehandler(); 

    public void go() 
    { 

     for(int b=0; b<number_button.length; b++) 
     { 
      number_button[b] = new JButton(number_button_name[b]); 
      number_button[b].addActionListener(handler); 
      number_panel.add(number_button[b]); 

     } 

     for(int i=0; i<math_button.length;i++) 
     { 
      math_button[i] = new JButton(name[i]); 
      math_button[i].addActionListener(handler); 
      math_panel.add(math_button[i]); 

     } 


     frame.getContentPane().add(BorderLayout.NORTH, math_input); 
     frame.getContentPane().add(BorderLayout.SOUTH, math_panel); 
     frame.getContentPane().add(BorderLayout.CENTER, number_panel); 
     frame.setSize(400,400); 
     frame.setVisible(true); 



    } 

    //Method to handle the math and return the results of whichever 'button' was pressed 
    static int Math(String button_num, int first_num, int second_num) 
    { 
     int total = 0; 
     if(button_num == "Add") 
     { 
      total = first_num + second_num; 
     } 

     else if (button_num == "Mulitply") //multiply 
     { 
      total = first_num * second_num; 
     } 

     else if (button_num == "Divide") //divide 
     { 
      total = first_num/second_num; 
     } 

     else if (button_num == "Substract") //subtract 
     { 
      total = first_num - second_num; 
     } 

     else if (button_num == "Equals") //subtract 
     { 
      total = total; 
     } 

     return total; 
    } 

    //Action Events - Code that is triggered once the associated button is clicked 


    public class thehandler implements ActionListener 
    { 
     public void actionPerformed(ActionEvent event) 
     { 


      for (int h = 0; h <math_button.length; h++) 
      { 
       if(event.getSource()==math_button[h]) 
       { 
        int firstn = Integer.parseInt(math_input.getText()); 
        math_input.setText(""); 
        int secondn = Integer.parseInt(math_input.getText()); 

        System.out.println(calc.Math(math_button[h].getText(), firstn, secondn)); 

       } 

      } 

      for(int n=0; n<number_button.length; n++) 
      { 
       if(event.getSource()==number_button[n]) 
       { 
        String number_clicked = (number_button[n].getText()); 
        String number = math_input.getText(); 
        math_input.setText(number + number_clicked); 

       } 



      } 

     }  
    } 

} 

此代碼背後的想法是創建一個簡單的GUI,並允許用戶輸入數字的期望量,然後按「等於」按鈕以顯示結果。但是,如上所述,我在邏輯上遇到了問題。我可以從JTextField獲得第一個輸入的數字,第一個變量初始化後清除文本,但這是程序翻轉的地方。變量'second_num'作爲空白傳遞給'Math'方法(拋出錯誤),因爲這是我告訴ActionEvent要做的事情,以便更流暢地使用該程序,沒有用戶希望不得不保持清除使用計算器時的輸入框。

有人有什麼想法嗎?

謝謝

+1

你可能也想看看這個[使用'ScriptEngine']的示例(http://stackoverflow.com/a/7441804/418556) 。 –

回答

0

我看着你的代碼,但它聽起來很複雜。我建議你使用像Netbeans這樣的IDE。創建swing應用程序。要添加兩個號碼,所有你需要做的是爲遵循

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { 
     int input_1 = Integer.parseInt(jTextField1.getText()); 
     int input_2 = Integer.parseInt(jTextField2.getText());   
     jTextField3.setText(String.valueOf((input_1+input_2))); 
} 

http://i.stack.imgur.com/1G4v7.jpg

2
int firstn = Integer.parseInt(math_input.getText()); 
math_input.setText(""); 
int secondn = Integer.parseInt(math_input.getText()); 

究竟你有什麼期望上面的行呢?您正在從math_input獲取文本。然後將其設置爲空字符串。通過立即得到字符串,你期望獲得除了空字符串以外的東西嗎?

正確的做法是:

處理程序被稱爲
  • 第一次(即一個「數學」按鈕被點擊)收集輸入。將它存儲在某個地方。
  • 下一次這個處理程序將被調用,你將有你的下一個輸入。依此類推,直到用戶點擊「=」來評估整個表達式。

建議:如果您是java的新手,您可能會首先在命令行創建一個計算器更容易。計算器的功能不需要GUI。在命令行中收集輸入更簡單。如果你得到這個工作,那麼你可以繼續更多花式的東西,像Swing