2016-01-13 29 views
1

我創建了一個簡單的計算器,其中我在兩個不同的文本字段中輸入了兩個操作數,它運行良好。但是我需要從同一個文本字段獲取兩個輸入。我應該做什麼改變?如何從AWT/SWING中的相同文本字段輸入兩位數

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

class Calculation_ActionEvent extends JFrame implements ActionListener 
{ 
    JFrame f; 
    JLabel l; 
    JTextField tf1, tf2, tf3; 
    JButton b1, b2, b3, b4 ,b5, b6; 
    Calculation_ActionEvent(String s) 
    { 
     f = new JFrame("Calculation"); 
     f.setLayout(null); 
     l = new JLabel("Enter two numbers : "); 
     tf1 = new JTextField(); 
     tf2 = new JTextField(); 
     tf3 = new JTextField(); 
     b1 = new JButton("+"); 
     b2 = new JButton("-"); 
     b3 = new JButton("*"); 
     b4 = new JButton("/"); 
     b5 = new JButton("equals"); 
     b6 = new JButton("C"); 
     f.add(l); 
     f.add(tf1); 
     f.add(tf2); 
     f.add(tf3); 
     f.add(b1); 
     f.add(b2); 
     f.add(b3); 
     f.add(b4); 
     f.add(b5); 
     f.add(b6); 
     tf1.setBounds(180,100,50,30); 
     tf2.setBounds(320,100,50,30); 
     tf3.setBounds(250,420,50,30); 
     b1.setBounds(250,180,50,30); 
     b2.setBounds(350,260,50,30); 
     b3.setBounds(150,260,50,30); 
     b4.setBounds(250,340,50,30); 
     b5.setBounds(230,260,90,30); 
     b6.setBounds(250,100,50,30); 
     b1.addActionListener(this); 
     b2.addActionListener(this); 
     b3.addActionListener(this); 
     b4.addActionListener(this); 
     //b5.addActionListener(this); 
     b6.addActionListener(this); 
     f.setSize(550,550); 
     f.setVisible(true); 
    } 

    public void actionPerformed(ActionEvent e) 
    { 
     int num1= Integer.parseInt(tf1.getText()); 
     int num2= Integer.parseInt(tf2.getText()); 
     String s1 = e.getActionCommand(); 
     if(s1.equals("C")) 
     { 
      tf1.setText("0"); 
      tf2.setText("0"); 
      tf3.setText("0"); 
     } 
     else 
     { 
     if(s1=="+") 
     { 
      tf3.setText(String.valueOf(num1+num2)); 
     } 
     if(s1=="-") 
     { 
      tf3.setText(String.valueOf(num1-num2)); 
     } 
     if(s1=="*") 
     { 
      tf3.setText(String.valueOf(num1*num2)); 
     } 
     if(s1=="/") 
     { 
      tf3.setText(String.valueOf(num1/num2)); 
     } 
     } 
} 

    public static void main(String... s) 
    { 
     new Calculation_ActionEvent("Calculation"); 
    } 
} 
+0

其實這是我的第一個程序在Swing ..請幫助我通過它。 – AspiringCoderNeo

+0

你的意思是說像「4 + 4」,「8」(即一個文本字段中的兩個輸入)而不是「4」,「4」,「8」?你應該包括一個例子,它會有所幫助。 –

+0

我的意思是有一個文本字段,首先我要寫一個操作數,然後按下運算符,然後按下同一文本字段中的第二個操作數,同時將第一個值存儲在某個變量中,然後將結果打印在第二個文本中領域。例如:在文本框中鍵入5,按下'+'並在同一文本框中鍵入3 ..以獲得第二個文本框中的答案8 – AspiringCoderNeo

回答

0

這是類似於你要找的東西嗎?我爲你做了一個快速版本。

簡單地說,我最後的算(+-*/)在String變量(lastAction),該total存儲爲int。當按下「等於」時,我計算出總數並重置total變量和lastAction。當按下一個操作符時,我改變總數,但將最後一個值保存在一個變量中。

package cf.pgmann.calc; 

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

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JTextField; 

class Calculation_ActionEvent extends JFrame implements ActionListener { 
    JFrame f; 
    JLabel l; 
    JTextField tf1, tf3; 
    JButton b1, b2, b3, b4, b5, b6; 

    Calculation_ActionEvent(String s) { 
     f = new JFrame(s); 
     f.setLayout(null); 
     l = new JLabel("Enter two numbers : "); 
     tf1 = new JTextField(); 
     // tf2 = new JTextField(); 
     tf3 = new JTextField(); 
     b1 = new JButton("+"); 
     b2 = new JButton("-"); 
     b3 = new JButton("*"); 
     b4 = new JButton("/"); 
     b5 = new JButton("equals"); 
     b6 = new JButton("C"); 
     f.add(l); 
     f.add(tf1); 
     f.add(tf3); 
     f.add(b1); 
     f.add(b2); 
     f.add(b3); 
     f.add(b4); 
     f.add(b5); 
     f.add(b6); 
     tf1.setBounds(160, 100, 250, 30); 
     // tf2.setBounds(320,100,50,30); 
     tf3.setBounds(250, 420, 50, 30); 
     b1.setBounds(250, 180, 50, 30); 
     b2.setBounds(350, 260, 50, 30); 
     b3.setBounds(150, 260, 50, 30); 
     b4.setBounds(250, 340, 50, 30); 
     b5.setBounds(230, 260, 90, 30); 
     b6.setBounds(420, 100, 50, 30); 
     b1.addActionListener(this); 
     b2.addActionListener(this); 
     b3.addActionListener(this); 
     b4.addActionListener(this); 
     b5.addActionListener(this); 
     b6.addActionListener(this); 
     f.setSize(550, 550); 
     f.setVisible(true); 
    } 

    int total = 0; 
    String lastAction = ""; 

    public void actionPerformed(ActionEvent e) { 
     int num = 0; 
     try { 
      num = Integer.parseInt(tf1.getText()); 
     } catch(NumberFormatException ex) {} 
     String s1 = e.getActionCommand(); 
     if (s1.equals("C")) { 
      tf1.setText(""); 
      tf3.setText("0"); 
      tf1.requestFocus(); 
      total = 0; 
     } else if (s1.equals("equals")) { 
      tf3.setText(String.valueOf(calc(total, lastAction, num))); 
      tf1.setText(""); 
      tf1.requestFocus(); 
      total = 0; 
     } else { 
      total = total==0 ? num : calc(total, lastAction, num); 
      tf3.setText(String.valueOf(total)); 
      tf1.setText(""); 
      tf1.requestFocus(); 
      lastAction = s1; 
     } 
    } 

    private int calc(int num1, String action, int num2) { 
     switch (action) { 
     case "+": 
      return num1 + num2; 
     case "-": 
      return num1 - num2; 
     case "*": 
      return num1 * num2; 
     case "/": 
      return num1/num2; 
     default: 
      return num1; 
     } 
    } 

    public static void main(String... s) { 
     new Calculation_ActionEvent("Calculator"); 
    } 
} 
0

剛剛獲得來自兩個文本框中的值,並執行所有的計算加法,乘法,減法,在不同的可變除法和存儲和設定,在文本框的變量,你不能直接執行下textbox.ex行動

String val1=textbox1.gettext(); 
String val2=textbox2.gettext(); 
int addition=Integer.ParseInt(val1)+Integer.ParseInt(val2); 
textbox3.settext(addition.toString()); 
相關問題