怎么用键盘事件实现JAVA编计算器
2016-11-30
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
public class CalculatorGUI {
private Frame f;
private Panel p1, p2;
private JButton b0, b1, b2, b3, b4, b5, b6, b7, b8, b9;
private JButton bPoint, bAdd, bDec, bMul, bDiv, bCal, Bbac, Bce, Bsqr, BZF;
private TextField tf;
private String s, op;
private Calculator cal = new Calculator();
private boolean ifOp;
private float Num, Num1, Num2;
public CalculatorGUI() {
f = new Frame("A Calculator");
p1 = new Panel();
p2 = new Panel();
b0 = new JButton("0");
b1 = new JButton("1");
b2 = new JButton("2");
b3 = new JButton("3");
b4 = new JButton("4");
b5 = new JButton("5");
b6 = new JButton("6");
b7 = new JButton("7");
b8 = new JButton("8");
b9 = new JButton("9");
bPoint = new JButton(".");
bAdd = new JButton("+");
bDec = new JButton("-");
bMul = new JButton("*");
bDiv = new JButton("/");
bCal = new JButton("=");
Bbac = new JButton("backspace");
Bce = new JButton("CE");
Bsqr = new JButton("sqrt");
BZF = new JButton("+/-");
tf = new TextField(50);
}
public void launchFrame() {
f.setSize(400, 210);
f.setResizable(false);
f.addWindowListener(new myWindowListener());
f.setLayout(new FlowLayout(FlowLayout.CENTER));
p1.add(tf);
f.add(p1, BorderLayout.NORTH);
p2.setLayout(new GridLayout(5, 4));
b0.addActionListener(new setlabeltext_actionlistener());
b1.addActionListener(new setlabeltext_actionlistener());
b2.addActionListener(new setlabeltext_actionlistener());
b3.addActionListener(new setlabeltext_actionlistener());
b4.addActionListener(new setlabeltext_actionlistener());
b5.addActionListener(new setlabeltext_actionlistener());
b6.addActionListener(new setlabeltext_actionlistener());
b7.addActionListener(new setlabeltext_actionlistener());
b8.addActionListener(new setlabeltext_actionlistener());
b9.addActionListener(new setlabeltext_actionlistener());
b0.addKeyListener(new KeyBoard());
b1.addKeyListener(new KeyBoard());
b2.addKeyListener(new KeyBoard());
b3.addKeyListener(new KeyBoard());
b4.addKeyListener(new KeyBoard());
b5.addKeyListener(new KeyBoard());
b6.addKeyListener(new KeyBoard());
b7.addKeyListener(new KeyBoard());
b8.addKeyListener(new KeyBoard());
b9.addKeyListener(new KeyBoard());
Bbac.addActionListener(new setoperator_actionlistener());
bPoint.addActionListener(new setlabeltext_actionlistener());
bAdd.addActionListener(new setoperator_actionlistener());
bDec.addActionListener(new setoperator_actionlistener());
bMul.addActionListener(new setoperator_actionlistener());
bDiv.addActionListener(new setoperator_actionlistener());
bCal.addActionListener(new setoperator_actionlistener());
Bce.addActionListener(new setoperator_actionlistener());
Bsqr.addActionListener(new setoperator_actionlistener());
BZF.addActionListener(new setoperator_actionlistener());
Bbac.addKeyListener(new KeyBoard());
bPoint.addKeyListener(new KeyBoard());
bAdd.addKeyListener(new KeyBoard());
bDec.addKeyListener(new KeyBoard());
bMul.addKeyListener(new KeyBoard());
bDiv.addKeyListener(new KeyBoard());
bCal.addKeyListener(new KeyBoard());
Bce.addKeyListener(new KeyBoard());
Bsqr.addKeyListener(new KeyBoard());
BZF.addKeyListener(new KeyBoard());
p2.add(b7);
p2.add(b8);
p2.add(b9);
p2.add(bAdd);
p2.add(b4);
p2.add(b5);
p2.add(b6);
p2.add(bDec);
p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(bMul);
p2.add(b0);
p2.add(bPoint);
p2.add(bCal);
p2.add(bDiv);
p2.add(Bbac);
p2.add(Bce);
p2.add(Bsqr);
p2.add(BZF);
//p2.addKeyListener(new KeyBoard());
f.add(p2, BorderLayout.SOUTH);
f.addKeyListener(new KeyBoard());
f.setVisible(true);
f.setFocusable(true);
}
class myWindowListener extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
class setlabeltext_actionlistener implements ActionListener {
public void actionPerformed(ActionEvent e) {
JButton tempB = (JButton) e.getSource();
s = tempB.getText();
settextfieldtext();
}
}
class setoperator_actionlistener implements ActionListener {
public void actionPerformed(ActionEvent e) {
JButton tempB = (JButton) e.getSource();
op = tempB.getText();
if (op.equals("+")) {
tf.setText(cal.opAdd(tf.getText()));
ifOp = true;
} else if (op.equals("-")) {
tf.setText(cal.opSub(tf.getText()));
ifOp = true;
} else if (op.equals("*")) {
tf.setText(cal.opmul(tf.getText()));
ifOp = true;
} else if (op.equals("/")) {
tf.setText(cal.opdiv(tf.getText()));
ifOp = true;
} else if (op.equals("CE")) {
tf.setText(cal.opce(tf.getText()));
} else if (op.equals("=")) {
tf.setText(cal.opEquals(tf.getText()));
ifOp = true;
} else if (op.equals("sqrt")) {
tf.setText(cal.opsqrt(tf.getText()));
ifOp = true;
} else if (op.equals("+/-")) {
tf.setText(cal.opzf(tf.getText()));
ifOp = true;
} else if (op.equals("backspace")) {
tf.setText(cal.opbac(tf.getText()));
ifOp = false;
}
}
}
public void settextfieldtext_temp() {
if (tf.getText().length() < 15
&& (tf.getText().indexOf(".") == -1 || !s.equals("."))) {
tf.setText(tf.getText() + s);
} else {
tf.setText((tf.getText() + s).substring(0, 15));
}
}
public void settextfieldtext() {
if (ifOp) {
ifOp = false;
tf.setText("");
settextfieldtext_temp();
} else {
settextfieldtext_temp();
}
}
class KeyBoard implements KeyListener {
char ysf2 = ' ';// 存储运算符
public void keyPressed(KeyEvent ke) {
}
// 弹起事件
public void keyReleased(KeyEvent ke) {
System.out.println(ke.getKeyChar());
// 从键盘读入数字
if (ke.getKeyChar() == '0') {
b0.doClick();
}
if (ke.getKeyChar() == '1') {
b1.doClick();
}
if (ke.getKeyChar() == '2') {
b2.doClick();
}
if (ke.getKeyChar() == '3') {
b3.doClick();
}
if (ke.getKeyChar() == '4') {
b4.doClick();
}
if (ke.getKeyChar() == '5') {
b5.doClick();
}
if (ke.getKeyChar() == '6') {
b6.doClick();
}
if (ke.getKeyChar() == '7') {
b7.doClick();
}
if (ke.getKeyChar() == '8') {
b8.doClick();
}
if (ke.getKeyChar() == '9') {
b9.doClick();
}
// 从键盘输入小数点
if (ke.getKeyChar() == '.')
bPoint.doClick();
// 加法
if (ke.getKeyChar() == '+') {
bAdd.doClick();
}
// 减法
if (ke.getKeyChar() == '-') {
bDec.doClick();
}
// 乘法
if (ke.getKeyChar() == '*') {
bMul.doClick();
}
// 除法
if (ke.getKeyChar() == '/') {
bDiv.doClick();
}
if (ke.getKeyChar() == '=') {
bCal.doClick();
}
if (ke.getKeyChar() == KeyEvent.VK_BACK_SPACE) {
Bbac.doClick();
}
if (ke.getKeyChar() == KeyEvent.VK_ENTER) {
bCal.doClick();
}
}
public void keyTyped(KeyEvent ke) {
}
}
// 按下事件
public static void main(String[] args) {
CalculatorGUI calculator = new CalculatorGUI();
calculator.launchFrame();
}
}
你试试看行不行