0
我使用java.the製作了一個簡單的計算器,第一個Jtextfield取第一個值。第二個Jtextfield取第二個值,第三個Jtextfield在GUI上按下+, - ,*,/按鈕後顯示計算出的答案。如何使用gui按鈕在不同的JtextFields中編寫文本。如何將gui按鈕的焦點更改爲不同的JtextFields?
我不能使用超過1個JTextField.how中的GUI按鈕來輸入值,以獲得GUI按鈕以將文本輸入到兩個文本字段?
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ui implements ActionListener {
public JButton btn1;
public JButton btn2;
public JButton btn3;
public JButton btn4;
public JButton btn5;
public JButton btn6;
public JButton btn7;
public JButton btn8;
public JButton btn9;
public JButton btn0;
public JButton btnAdd;
public JButton btnSub;
public JButton btnMultiply;
public JButton btnPoint;
public JButton btnDivide;
public JButton btnEquals;
public JButton btnClear;
public JButton btnExit;
public static JTextField text;
public static JTextField text2;
public static JTextField text3;
public static void ui() {
ui u1= new ui();
JFrame f = new JFrame();
f.setSize(300,500);
f.setTitle("my calculator");
f.setResizable(false);
JPanel p = new JPanel();
BoxLayout b = new BoxLayout(p,BoxLayout.Y_AXIS);
p.setLayout(b);
Font fo = new Font("ARIAL",Font.CENTER_BASELINE,30);
Font fo1 = new Font("ARIAL",Font.ITALIC,20);
u1.text = new JTextField(10);
u1.text.setSize(300,100);
u1.text.setText("0");
u1.text.setFont(fo1);
u1.text.setBackground(Color.gray);
p.add(u1.text);
u1.text2 = new JTextField(10);
u1.text2.setSize(300,100);
u1.text2.setText("0");
u1.text2.setFont(fo1);
u1.text2.setBackground(Color.gray);
p.add(u1.text2);
u1.text3 = new JTextField(10);
u1.text3.setSize(300,100);
u1.text3.setText("0");
u1.text3.setFont(fo1);
u1.text3.setBackground(Color.gray);
p.add(u1.text3);
JPanel p1 = new JPanel(new GridLayout(6,3));
u1.btn1 = new JButton("1");
u1.btn1.setFont(fo);
p1.add(u1.btn1);
u1.btn1.addActionListener(u1);
u1.btn2 = new JButton("2");
u1.btn2.setFont(fo);
p1.add(u1.btn2);
u1.btn2.addActionListener(u1);
u1.btn3 = new JButton("3");
u1.btn3.setFont(fo);
p1.add(u1.btn3);
u1.btn3.addActionListener(u1);
u1.btn4 = new JButton("4");
u1.btn4.setFont(fo);
p1.add(u1.btn4);
u1.btn4.addActionListener(u1);
u1.btn5 = new JButton("5");
u1.btn5.setFont(fo);
p1.add(u1.btn5);
u1.btn5.addActionListener(u1);
u1.btn6 = new JButton("6");
u1.btn6.setFont(fo);
p1.add(u1.btn6);
u1.btn6.addActionListener(u1);
u1.btn7 = new JButton("7");
u1.btn7.setFont(fo);
p1.add(u1.btn7);
u1.btn7.addActionListener(u1);
u1.btn8 = new JButton("8");
u1.btn8.setFont(fo);
p1.add(u1.btn8);
u1.btn8.addActionListener(u1);
u1.btn9 = new JButton("9");
u1.btn9.setFont(fo);
p1.add(u1.btn9);
u1.btn9.addActionListener(u1);
u1.btnEquals = new JButton("=");
u1.btnEquals.setFont(fo);
u1.btnEquals.setBackground(Color.getHSBColor(30,100,100));
p1.add(u1.btnEquals);
u1.btnEquals.addActionListener(u1);
u1.btn0 = new JButton("0");
u1.btn0.setFont(fo);
p1.add(u1.btn0);
u1.btn0.addActionListener(u1);
u1.btnPoint = new JButton(".");
u1.btnPoint.setFont(fo);
u1.btnPoint.setBackground(Color.getHSBColor(30,100,100));
p1.add(u1.btnPoint);
u1.btnPoint.addActionListener(u1);
u1.btnAdd = new JButton("+");
u1.btnAdd.setFont(fo);
u1.btnAdd.setBackground(Color.getHSBColor(30,100,100));
p1.add(u1.btnAdd);
u1.btnAdd.addActionListener(u1);
u1.btnSub = new JButton("-");
u1.btnSub.setFont(fo);
u1.btnSub.setBackground(Color.getHSBColor(30,100,100));
p1.add(u1.btnSub);
u1.btnSub.addActionListener(u1);
u1.btnMultiply = new JButton("*");
u1.btnMultiply.setFont(fo);
u1.btnMultiply.setBackground(Color.getHSBColor(30,100,100));
p1.add(u1.btnMultiply);
u1.btnMultiply.addActionListener(u1);
u1.btnDivide = new JButton("/");
u1.btnDivide.setFont(fo);
u1.btnDivide.setBackground(Color.getHSBColor(30,100,100));
p1.add(u1.btnDivide);
u1.btnDivide.addActionListener(u1);
u1.btnClear = new JButton("clear");
u1.btnClear.setFont(fo);
u1.btnClear.setBackground(Color.getHSBColor(30,100,100));
p1.add(u1.btnClear);
u1.btnClear.addActionListener(u1);
u1.btnExit = new JButton("exit");
u1.btnExit.setFont(fo);
u1.btnExit.setBackground(Color.getHSBColor(30,100,100));
p1.add(u1.btnExit);
u1.btnExit.addActionListener(u1);
p.add(p1);
f.add(p);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==this.btnAdd){
maths.add();
}
else if (e.getSource()==this.btnSub){
maths.sub();
}
else if(e.getSource()==this.btnMultiply){
maths.multiply();
}
else if(e.getSource()==this.btnDivide){
try {
maths.divide();
} catch (Exception e1) {
e1.printStackTrace();
}
}
else if(e.getSource()==this.btnClear){
text.setText("0");
text2.setText("0");
text3.setText("0"); }
else if(e.getSource()==this.btnExit){
System.exit(0);
}
else if (e.getSource()==this.btn1){
if(ui.text.hasFocus()==true){
ui.text.setText("1");
}
else{
ui.text2.setText("1");
}
}
}
}
public class maths {
public static void add(){
float a=0,b=0,c=0;
a=Float.parseFloat(ui.text.getText());
b=Float.parseFloat(ui.text2.getText());
c=a+b;
ui.text3.setText(String.valueOf(c));
}
public static void sub(){
float a=0,b=0,c=0;
a=Float.parseFloat(ui.text.getText());
b=Float.parseFloat(ui.text2.getText());
c=a-b;
ui.text3.setText(String.valueOf(c));
}
public static void multiply(){
float a=0,b=0,c=0;
a=Float.parseFloat(ui.text.getText());
b=Float.parseFloat(ui.text2.getText());
c= a*b;
ui.text3.setText(String.valueOf(c));
}
public static void divide()throws Exception{
float a=0,b=0,c=0;
a=Float.parseFloat(ui.text.getText());
b=Float.parseFloat(ui.text2.getText());
c=a/b;
ui.text3.setText(String.valueOf(c));
}
public class demo {
public static void main(String[] args) {
ui.ui();
}
}
的可能的複製[爪哇確定哪些文本框具有對焦](https://stackoverflow.com/questions/20409024/java-determine-which-textfield-has-focus) – Juliane