按键为操作符
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Work2 extends JFrame implements ActionListener {
private JButton bt_add,bt_sub,bt_mul,bt_div;
private JTextField num1,num2,result;
public Work2(){
super("计算器");
setSize(400,200);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
bt_add = new JButton("加");
bt_sub = new JButton("减");
bt_mul = new JButton("乘");
bt_div = new JButton("除");
num1 = new JTextField("",5);
num2 = new JTextField("",5);
result = new JTextField("",8);
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
p1.add(num1);
p1.add(num2);
p1.add(result);
p2.add(bt_add);
p2.add(bt_sub);
p2.add(bt_mul);
p2.add(bt_div);
add(p1, BorderLayout.NORTH);
p2.setLayout(new GridLayout(1,4));
add(p2,BorderLayout.CENTER);
setVisible(true);
bt_add.addActionListener(this);
bt_sub.addActionListener(this);
bt_mul.addActionListener(this);
bt_div.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
double n1,n2;
n1 = getnum(num1);
n2 = getnum(num2);
if(e.getActionCommand().equals("加")){
result.setText(compute(n1,n2,'+'));
return;
}
if(e.getActionCommand().equals("减")){
result.setText(compute(n1,n2,'-'));
return;
}
if(e.getActionCommand().equals("乘")){
result.setText(compute(n1,n2,'*'));
return;
}
if(e.getActionCommand().equals("除")){
result.setText(compute(n1,n2,'/'));
return;
}
}
public double getnum(JTextField jf){
double x;
String s = jf.getText().trim();
x = Double.parseDouble(s);
return x;
}
public String compute(double x,double y,char op) {
double r = 0;
if (y == 0 && op == '/')
return "除零错";
switch (op) {
case '+':
r = x + y;
break;
case '-':
r = x - y;
break;
case '*':
r = x * y;
break;
case '/':
r = x / y;
break;
}
return String.valueOf(r);
}
public static void main(String[] argv){
new Work2();
}
}
按键为操作数和符号
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Work1 extends JFrame implements ActionListener {
private JTextField op1,op2,result;
private JLabel opChar;
private JButton bt_c;
private String[] bt_Label = {"1","2","3","4","5","6","7","8","9","0","+","-","*","/","="};
private JButton[] bt = new JButton[bt_Label.length];
private boolean isOp1 = true;
public Work1(){
super("计算器");
setSize(500,300);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p1 = new JPanel();
op1 = new JTextField("",5);
opChar = new JLabel("?");
op2 = new JTextField("",5);
bt_c = new JButton("C");
result = new JTextField("",10);
p1.add(op1);
p1.add(opChar);
p1.add(op2);
p1.add(new JLabel("="));
p1.add(result);
p1.add(bt_c);
op1.setEditable(false);
op2.setEditable(false);
result.setEditable(false);
bt_c.addActionListener(this);
add(p1, BorderLayout.NORTH);
JPanel p2 = new JPanel();
p2.setLayout(new GridLayout(3,5,5,5));
for(int i=0;i<bt.length;i++){
bt[i] = new JButton(bt_Label[i]);
bt[i].addActionListener(this);
p2.add(bt[i]);
}
add(p2,BorderLayout.CENTER);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(e.getActionCommand().equals("C")){
op1.setText("");
op2.setText("");
opChar.setText("?");
result.setText("");
isOp1 = true;
return;
}
if("0123456789".indexOf(e.getActionCommand()) >= 0){
if(isOp1 && op1.getText().length()<5){
op1.setText(op1.getText()+e.getActionCommand());
}
else if(!isOp1 && op2.getText().length()<5){
op2.setText(op2.getText()+e.getActionCommand());
}
return;
}
if("+-*/".indexOf(e.getActionCommand())>=0){
opChar.setText(e.getActionCommand());
isOp1=false;
return;
}
if(e.getActionCommand().equals("=")) {
isOp1 = true;
if (op1.getText().equals("") || op2.getText().equals("")) {
result.setText("不能计算");
return;
}
int x, y;
char op = opChar.getText().charAt(0);
x = Integer.parseInt(op1.getText());
y = Integer.parseInt(op2.getText());
result.setText(compute(x, y, op));
}
}
public String compute(int x,int y,char op) {
double r = 0;
if (y == 0 && op == '/')
return "除零错";
switch (op) {
case '+':
r = x + y;
break;
case '-':
r = x - y;
break;
case '*':
r = x * y;
break;
case '/':
r = x / y;
break;
}
return String.valueOf(r);
}
public static void main(String[] argv){
new Work1();
}
}