我有一个相当简单的计算器,我正在尝试将键绑定到 JButtons。我是Java新手,我知道的不多。我确实知道它涉及 ActionListener,但我不知道如何将它变成我目前拥有的东西。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
@SuppressWarnings("serial")
public class Calculator2 extends JFrame implements ActionListener {
// Declare the GUI objects and variables HERE
JTextField ansText;
JButton b1, b2, b3, b4, b5, b6, b7, b8, b9, b0, plus, minus, multi, div,
equal, clear;
JPanel p1, p2, p3;
Double val1 = 0.0, val2 = 0.0, answer = 0.0;
int operator = 0;
public static void main(String[] args) {
new Calculator2();
}
public Calculator2() {
// GUI Creation Code goes HERE
ansText = new JTextField("", 7);
ansText.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
ansText.addKeyListener(new KeyAdapter() { //Allow only numbers in ansText
public void keyTyped(KeyEvent e) {
char c = e.getKeyChar();
if (((c < '0') || (c > '9')) && (c != KeyEvent.VK_BACK_SPACE)) {
e.consume();
}
}
});
b1 = new JButton("1");
b1.addActionListener(this);
b2 = new JButton("2");
b2.addActionListener(this);
b3 = new JButton("3");
b3.addActionListener(this);
b4 = new JButton("4");
b4.addActionListener(this);
b5 = new JButton("5");
b5.addActionListener(this);
b6 = new JButton("6");
b6.addActionListener(this);
b7 = new JButton("7");
b7.addActionListener(this);
b8 = new JButton("8");
b8.addActionListener(this);
b9 = new JButton("9");
b9.addActionListener(this);
b0 = new JButton("0");
b0.addActionListener(this);
plus = new JButton("+");
plus.addActionListener(this);
minus = new JButton("-");
minus.addActionListener(this);
multi = new JButton("*");
multi.addActionListener(this);
div = new JButton("/");
div.addActionListener(this);
equal = new JButton("=");
equal.addActionListener(this);
clear = new JButton("C");
clear.addActionListener(this);
this.setLayout(new BorderLayout());
p1 = new JPanel();
this.add(p1, BorderLayout.NORTH);
p1.setLayout(new GridLayout(0, 1, 2, 2));
p1.add(ansText);
p2 = new JPanel();
this.add(p2, BorderLayout.CENTER);
p2.setLayout(new GridLayout(4, 3, 2, 2));
p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(plus);
p2.add(b4);
p2.add(b5);
p2.add(b6);
p2.add(minus);
p2.add(b7);
p2.add(b8);
p2.add(b9);
p2.add(multi);
p2.add(clear);
p2.add(b0);
p2.add(equal);
p2.add(div);
this.setSize(200, 250);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
//Number input
if (e.getSource() == b1) {
ansText.setText(ansText.getText() + b1.getText());
}
else if (e.getSource() == b2) {
ansText.setText(ansText.getText() + b2.getText());
}
else if (e.getSource() == b3) {
ansText.setText(ansText.getText() + b3.getText());
}
else if (e.getSource() == b4) {
ansText.setText(ansText.getText() + b4.getText());
}
else if (e.getSource() == b5) {
ansText.setText(ansText.getText() + b5.getText());
}
else if (e.getSource() == b6) {
ansText.setText(ansText.getText() + b6.getText());
}
else if (e.getSource() == b7) {
ansText.setText(ansText.getText() + b7.getText());
}
else if (e.getSource() == b8) {
ansText.setText(ansText.getText() + b8.getText());
}
else if (e.getSource() == b9) {
ansText.setText(ansText.getText() + b9.getText());
}
else if (e.getSource() == b0) {
ansText.setText(ansText.getText() + b0.getText());
}
//Operator input
else if (e.getSource() == plus) {
operator = 1;
val1 = Double.parseDouble(ansText.getText());
ansText.setText("");
}
else if (e.getSource() == minus) {
operator = 2;
val1 = Double.parseDouble(ansText.getText());
ansText.setText("");
}
else if (e.getSource() == multi) {
operator = 3;
val1 = Double.parseDouble(ansText.getText());
ansText.setText("");
}
else if (e.getSource() == div) {
operator = 4;
val1 = Double.parseDouble(ansText.getText());
ansText.setText("");
}
//Misc
else if (e.getSource() == equal) {
val2 = Double.parseDouble(ansText.getText());
if (operator == 1) {
answer = val1 + val2;
ansText.setText("" + answer);
} else if (operator == 2) {
answer = val1 - val2;
ansText.setText("" + answer);
} else if (operator == 3) {
answer = val1 * val2;
ansText.setText("" + answer);
} else if (operator == 4) {
answer = val1 / val2;
ansText.setText("" + answer);
}
}
else if (e.getSource() == clear) {
ansText.setText("");
val1 = 0.0;
val2 = 0.0;
answer = 0.0;
operator = 0;
}
}
}