2011-02-09 33 views
2

我已經完成了一個基本計算器作爲作業的一部分。 我目前使用兩個文本字段來輸入操作數。然後按操作員顯示答案。使用來自同一個jTextfield的兩位數字來執行計算

但是,這兩個整數都應該從相同的文本域輸入,所以我該怎麼做?

我已經考慮過使用數組或堆棧,但我不能讓它工作。

我在下面列出了我的代碼...計算器已準備好,並且一切正常,它可以進行乘法除法加法,並在文本區域中顯示答案,這兩個數字將被添加或減去...被輸入到兩個獨立的字段中,一旦按下運算符並完成計算(Reverse Polish Notation),然而,我怎樣才能消除第二個字段的使用,並且只需在文本字段中輸入第一個值,按enter然後插入第二個字段值輸入到相同的文本字段並按下回車鍵。將第一個保存爲valA,第二個保存爲val B(整數)。但它必須是相同的文本框。

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 

/* 
* Calcu.java 
* 
* Created on Feb 9, 2011, 10:11:37 PM 
*/ 

/** 
* 
* @author Halaseh 
*/ 
public class Calcu extends javax.swing.JFrame { 

    /** Creates new form Calcu */ 
    public Calcu() { 
     initComponents(); 
    } 

    /** This method is called from within the constructor to 
    * initialize the form. 
    * WARNING: Do NOT modify this code. The content of this method is 
    * always regenerated by the Form Editor. 
    */ 
    @SuppressWarnings("unchecked") 
    // <editor-fold defaultstate="collapsed" desc="Generated Code"> 
    private void initComponents() { 

     jScrollPane1 = new javax.swing.JScrollPane(); 
     jTextArea1 = new javax.swing.JTextArea(); 
     jTextField1 = new javax.swing.JTextField(); 
     jTextField2 = new javax.swing.JTextField(); 
     jButton1 = new javax.swing.JButton(); 
     jButton2 = new javax.swing.JButton(); 
     jButton3 = new javax.swing.JButton(); 
     jButton4 = new javax.swing.JButton(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

     jTextArea1.setColumns(20); 
     jTextArea1.setRows(5); 
     jScrollPane1.setViewportView(jTextArea1); 

     jTextField1.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       jTextField1ActionPerformed(evt); 
      } 
     }); 

     jTextField2.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       jTextField2ActionPerformed(evt); 
      } 
     }); 

     jButton1.setText("+"); 
     jButton1.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       jButton1ActionPerformed(evt); 
      } 
     }); 

     jButton2.setText("-"); 
     jButton2.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       jButton2ActionPerformed(evt); 
      } 
     }); 

     jButton3.setText("*"); 
     jButton3.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       jButton3ActionPerformed(evt); 
      } 
     }); 

     jButton4.setText("/"); 
     jButton4.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       jButton4ActionPerformed(evt); 
      } 
     }); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
     getContentPane().setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addContainerGap() 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) 
         .addComponent(jTextField1, javax.swing.GroupLayout.Alignment.LEADING) 
         .addComponent(jTextField2, javax.swing.GroupLayout.Alignment.LEADING) 
         .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup() 
          .addComponent(jButton3) 
          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
          .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 
         .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup() 
          .addComponent(jButton1) 
          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
          .addComponent(jButton2)))) 
       .addContainerGap(222, Short.MAX_VALUE)) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addContainerGap() 
       .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
       .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
       .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(jButton1) 
        .addComponent(jButton2)) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(jButton3) 
        .addComponent(jButton4)) 
       .addContainerGap(69, Short.MAX_VALUE)) 
     ); 

     pack(); 
    }// </editor-fold> 

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { 
int valA = Integer.parseInt(jTextField1.getText()); 
int valB = Integer.parseInt(jTextField2.getText());  // TODO add your handling code here: 

     int valC = valA+valB; 
     jTextArea1.append(Integer.toString(valC)); 
     jTextArea1.append("\n"); 


    } 

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) { 

int valA = Integer.parseInt(jTextField1.getText()); 
    } 

    private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) { 
int valB = Integer.parseInt(jTextField2.getText());  // TODO add your handling code here: 
    } 

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { 
int valA = Integer.parseInt(jTextField1.getText()); 
int valB = Integer.parseInt(jTextField2.getText());  // TODO add your handling code here: 

     int valC = valA-valB; 
     jTextArea1.append(Integer.toString(valC)); 
     jTextArea1.append("\n");  // TODO add your handling code here: 
    } 

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) { 
int valA = Integer.parseInt(jTextField1.getText()); 
int valB = Integer.parseInt(jTextField2.getText());  // TODO add your handling code here: 

     int valC = valA*valB; 
     jTextArea1.append(Integer.toString(valC)); 
     jTextArea1.append("\n");  // TODO add your handling code here: 
    } 

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) { 
int valA = Integer.parseInt(jTextField1.getText()); 
int valB = Integer.parseInt(jTextField2.getText());  // TODO add your handling code here: 

     int valC = valA/valB; 
     jTextArea1.append(Integer.toString(valC)); 
     jTextArea1.append("\n");  // TODO add your handling code here: 
    } 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String args[]) { 
     java.awt.EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       new Calcu().setVisible(true); 
      } 
     }); 
    } 

    // Variables declaration - do not modify 
    private javax.swing.JButton jButton1; 
    private javax.swing.JButton jButton2; 
    private javax.swing.JButton jButton3; 
    private javax.swing.JButton jButton4; 
    private javax.swing.JScrollPane jScrollPane1; 
    private javax.swing.JTextArea jTextArea1; 
    private javax.swing.JTextField jTextField1; 
    private javax.swing.JTextField jTextField2; 
    // End of variables declaration 

} 
+0

哦,我忘了提及,我今天剛開始學習Java,所以我認爲我從頭開始做這個計算器,我自己的代碼都做得很好。但我可能不知道如何單獨做事,所以例子可能會有幫助,謝謝! – Kay 2011-02-10 01:14:42

回答

0

要麼包含一個提交按鈕或監聽按下回車鍵,並有計劃從文本字段讀出數字,並將其存儲在一個變量。那麼你可以在其他地方顯示輸入的號碼?

不完全確定你的任務需要什麼,但我會考慮讓用戶輸入整個公式(「1 + 2」),然後解析它。

0

如果用戶改變主意,在輸入字符串後解析整個字符串將是最好的。嘗試將其轉換爲char數組,然後查找單個或連續的數字和操作數。

2

你將不得不讀入輸入並以某種方式解析字符串。

如果你只需要期待的形式1 + 3或4 * 5等的東西,你可以通過詢問是否存在檢查操作中的任何操作符號

if(inputString.indexOf("+") >= 0) { 
    //handle addition 
} 

然後你可以使用同時獲得數字:

String[] numbers = inputString.split("+"); 
Integer numberOne = Integer.parseInt(numbers[0]); 
... 

這使得很多的假設,並且一般而言,您應檢查是否有潛在的問題,但是這只是取決於如何指定項目。

+0

感謝您的回覆,兩個問題.split是做什麼的? 和我已經有opperands的按鈕。那麼我究竟會如何保存兩個值呢? 即時思考也許 string [] array; int valA = Integer.parseInt(array [0]); int valB = Integer.parseInt(array [1]); 我知道語法是錯誤的 – Kay 2011-02-10 02:01:18

0

可悲的是,我還不能發表評論...

但聽在一個JTextField回車鍵,使用此代碼:

EntryBox.addKeyListener(
      new KeyAdapter() { 

       public void keyPressed(KeyEvent e) { 
        int key = e.getKeyCode(); 
        if (key == KeyEvent.VK_ENTER) { 
         ActionToPerform(null); 
        } 
       } 
      }); 

EntryBox是我的JTextField,和ActionToPerform是一個方法當輸入密鑰被擊中時調用。在ActionToPerform中,您可以將EntryBox中的文本/數字複製到變量中。此外,我會建議結束ActionToPerform與

EntryBox.setText(""); 

這將清除文本框,因此它已準備好接受另一個數字。

相關問題