2013-03-13 82 views
1
在文本框輸入數字

我做了一個簡單的計算器通過按鈕GUI

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
public class Calculator extends JFrame implements ActionListener 
{ 
GridLayout layout = new GridLayout(5, 1); 
JLabel l1 = new JLabel("Number 1:"); 
JLabel l2 = new JLabel("Number 2:"); 
JLabel l3 = new JLabel("Answer:"); 
JTextField t1 = new JTextField(30); 
JTextField t2 = new JTextField(30); 
JTextField t3 = new JTextField(30); 
JButton add = new JButton("+"); 
JButton sub = new JButton("-"); 
JButton mul = new JButton("*"); 
JButton div= new JButton("/"); 
Float ans; 

public Calculator() 
    { 
    super("Calculator"); 
    setSize(250, 200); 
    add(l1); 
    add(t1); 
    add(l2); 
    add(t2); 
    add(l3); 
    add(t3); 
    add(add); 
    add(sub); 
    add(mul); 
    add(div); 
    setLayout(layout); 
    add.addActionListener(this); 
    sub.addActionListener(this); 
    mul.addActionListener(this); 
    div.addActionListener(this); 
    setVisible(true); 
    } 

public void actionPerformed(ActionEvent e) 
    { 
String n1 = t1.getText(); 
String n2 = t2.getText(); 
Float num1 = Float.parseFloat(n1); 
Float num2 = Float.parseFloat(n2); 
Object clicked = e.getSource(); 

if(add == clicked) 
{ 
    t3.setText(String.valueOf(num1+num2)); 
} 

else if(sub == clicked) 
{ 
    t3.setText(String.valueOf(num1-num2)); 
} 

else if(mul == clicked) 
{ 
    t3.setText(String.valueOf(num1*num2)); 
} 

else 
{ 
if(num2 == 0) 
t3.setText("Can't Divide By Zero"); 
else 
t3.setText(String.valueOf(num1/num2)); 
    } 
} 
} 

和A類閱讀它

public class UseMyFrame 
{ 
    public static void main(String[] args) 
    { 
    Calculator calc = new Calculator(); 
    calc.setVisible(true); 
    } 
} 

我的問題是我想補充另一個特點,並把9按鈕1-9當按下時將它們各自的數字放在文本框中,但我不知道如何將它們設置爲出現在文本框中,我首先想要set.text,但我意識到按鈕如何知道在哪裏把它的號碼,因爲如果我set.text我需要把它放在textfield1或textfield 2.我想讓麻木er首先出現在textfield1中,然後在textfield2上(如果textfield1上已經有一個數字)

回答

1

因此,您需要創建一個布爾變量來幫助您跟蹤哪個字段將數字放入。然後在按鈕的操作中你可以用它來決定。

if(first){ 
     textField1.setText("1"); 
     first = false; 
    }else{ 
     textField2.setText("1"); 
     first = true; 
    } 

現在,這段代碼非常簡單,並沒有考慮所有的可能性。它只是在兩個領域之間切換。你可以把它擴展到你需要的東西。

+0

謝謝,我會盡力議員的建議,然後你的,如果它不適合我,非常感謝 – 2013-03-13 13:56:59

1

如果你只是想有個位數:

if(t1.getText().length() == 0) 
    t1.setText(...); 
else 
    t2.setText(...); 

更好:找出哪個文本字段具有當前focus(見的javadoc),並把數字在文本的末尾:

tFocused.setText(tFocused.getText() + digit) 
+0

謝謝,我目前正在製作按鈕,並且在我嘗試了所有建議之後,我會給你們一個關於我的進展的提示,非常感謝 – 2013-03-13 13:56:29

2

,但我不知道如何設置他們出現在文本框

您添加到JButton的Action應該擴展TextAction。 TextAction可以訪問上次關注的文本組件(因此您不必自己跟蹤這些信息)。您的代碼將如下所示:

public class AddDigitAction extends TextAction 
{ 
    public void actionPerformed(ActionEvent e) 
    { 
     JButton button = (JButton)e.getSource(); 
     String digit = button.getActionCommand(); 
     JTextComponent target = getTextComponent(e); 
     target.replaceSelection(digit); 
} 

您可以對所有按鈕使用相同的操作。 replaceSelection()方法是向文本字段添加文本的簡單方法。它將在文本字段的插入符的最後位置插入文本。

1

第一套全球INT小人= 0和全局字符串屏幕 然後在每個數字鍵把這些代碼(所有關於字符串串聯)

if(curs==0){ 
    screen="1"; // change the number for each button 
    jTextField1.setText(screen); 
    a=Double.parseDouble(screen); 
    curs++; 
    }else{ 
    screen=screen+"1"; // // change the number for each button 
    jTextField1.setText(screen); 
    a=Double.parseDouble(screen); 
    curs++; 
    }