JAVA写个简易的计算器。只要加减乘除四个运算。三个文本框分别为输入的两个字符和结果。四个按钮加减乘除 30
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class GS extends JFrame implements ActionListener {
private String[] str = { "7", "8", "9", "/", "sqrt",
"4", "5", "6", "*", "%",
"1", "2", "3", "-", "1/x",
"0", "+/-", ".", "+", "=" };
private JTextField tf_out;
private JButton jb_bk, jb_ce, jb_c;
private JButton[] jb_key;
private char ch = '#';
private boolean can = false;
private double num1;
public void creatGUI() {
tf_out = new JTextField();
tf_out.setHorizontalAlignment(JTextField.RIGHT);
tf_out.setColumns(18);
tf_out.setEditable(false); // 设置输出不可编辑
tf_out.setText("0");
this.add(tf_out, BorderLayout.NORTH);
JPanel p = new JPanel(new BorderLayout(3, 8));
JPanel p1 = new JPanel(new GridLayout(1, 3, 3, 10));
p.add(p1, "North");
jb_bk = new JButton("Backspace");
jb_bk.setForeground(Color.RED);
jb_bk.addActionListener(this);
jb_ce = new JButton("CE");
jb_ce.setForeground(Color.RED);
jb_ce.addActionListener(this);
jb_c = new JButton("C");
jb_c.setForeground(Color.RED);
jb_c.addActionListener(this);
p1.add(jb_bk);
p1.add(jb_ce);
p1.add(jb_c);
JPanel p2 = new JPanel(new GridLayout(4, 5, 3, 3)); p.add(p2, BorderLayout.CENTER);
jb_key = new JButton[str.length];
for (int i = 0; i < str.length; i++) {
jb_key[i] = new JButton(str[i]);
jb_key[i].addActionListener(this);
if (i == 3 || i == 8 || i == 13 || i == 18 || i == 19) {
jb_key[i].setForeground(Color.RED);
} else {
jb_key[i].setForeground(Color.BLUE);
}
p2.add(jb_key[i]);
}
this.add(p, BorderLayout.CENTER);
this.setTitle("计算器");
this.setIconImage(new ImageIcon("image/1.jpg").getImage());
this.setBackground(Color.LIGHT_GRAY);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBounds((1280 - 300) / 2, (768 - 200) / 2, 340, 260);
this.setVisible(true);
}
public static void main(String[] args) {
new GS().creatGUI();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String act = e.getActionCommand();
// 控制键
if (e.getSource() == jb_bk) {
if (tf_out.getText().length() > 1){
tf_out.setText(tf_out.getText().substring(0, tf_out.getText().length() - 1));
}else{
tf_out.setText("0");
}
return;
}else if (e.getSource() == jb_ce || e.getSource() == jb_c) {
tf_out.setText("0");
ch = '#';
return;
}
// 数字键
if (act == "0") {
if (!tf_out.getText().equals("0")) {
tf_out.setText(tf_out.getText());
}
} else if (act == "1" || act == "2" || act == "3" || act == "4" || act == "5"
|| act == "6" || act == "7" || act == "9") {
tf_out.setText(tf_out.getText());
}
// 运算符
if (act.equals("+/-")) {
if (tf_out.getText().charAt(0) != '-') {
tf_out.setText("-" + tf_out.getText());
} else {
tf_out.setText(tf_out.getText().substring(1));
}
} else if (act.equals(".")) {
tf_out.setText(tf_out.getText() + act);
ch = '#';
} else if (act != "1/x" && act.charAt(0) >= '0' && act.charAt(0) <= '9') {
if (can) {
tf_out.setText(act);
can = false;
} else {
try {
if (Double.parseDouble(tf_out.getText()) == 0) {
if (tf_out.getText().equals("0.")) {
tf_out.setText(tf_out.getText() + act);
} else {
tf_out.setText(act);
}
} else {
tf_out.setText(tf_out.getText() + act);
}
} catch (NumberFormatException e1) {
JOptionPane.showMessageDialog(null, "输入格式错误!", "警告!",JOptionPane.ERROR_MESSAGE);
}
}
} else if (act.equals("+") || act.equals("-") || act.equals("*")
|| act.equals("/")) {
if (ch != '#') {
try {
num1 = operation(num1, ch, Double.parseDouble(tf_out.getText()));
tf_out.setText(String.valueOf(num1));
ch = act.charAt(0);
can = true;
return;
} catch (NumberFormatException e1) {
JOptionPane.showMessageDialog(null, "输入格式错误!", "警告!",JOptionPane.ERROR_MESSAGE);
return;
}
} else {
try {
num1 = Double.parseDouble(tf_out.getText());
ch = act.charAt(0);
can = true;
return;
} catch (NumberFormatException e1) {
JOptionPane.showMessageDialog(null, "输入格式错误!", "警告!",JOptionPane.ERROR_MESSAGE);
return;
}
}
} else if (act.equals("sqrt")) {
try {
double num = (double) Math.sqrt(Double.parseDouble(tf_out.getText()));
tf_out.setText(String.valueOf(num));
can = true;
return;
} catch (NumberFormatException e1) {
JOptionPane.showMessageDialog(null, "输入格式错误!", "警告!",JOptionPane.ERROR_MESSAGE);
}
} else if (act.equals("1/x")) {
try {
double num = 1 / Double.parseDouble(tf_out.getText());
tf_out.setText(String.valueOf(num));
can = true;
return;
} catch (NumberFormatException e1) {
JOptionPane.showMessageDialog(null, "输入格式错误!", "警告!",JOptionPane.ERROR_MESSAGE);
return;
} catch (ArithmeticException e1) {
JOptionPane.showMessageDialog(null, "除0错误!", "警告!", JOptionPane.ERROR_MESSAGE);
}
} else if (act.equals("=")) {
can = true;
try {
if (ch == '#') {
return;
}
double num = Double.parseDouble(tf_out.getText());
num1 = operation(num1, ch, num);
tf_out.setText(String.valueOf(num1));
ch = '#';
return;
} catch (NumberFormatException e1) {
JOptionPane.showMessageDialog(null, "输入格式错误!", "警告!",JOptionPane.ERROR_MESSAGE);
return;
}
} else if (act.equals("%")) {
double num = Double.valueOf(tf_out.getText()).doubleValue();
tf_out.setText(String.valueOf(num1));
double sum = (num1 * num) / 100;
tf_out.setText(String.valueOf(sum));
return;
}
}
// 运算
public double operation(double a, char c, double b) {
double sum;
switch (c) {
case '+':
sum = a + b;
break;
case '-':
sum = a - b;
break;
case '*':
sum = a * b;
break;
case '/':
if (b == 0) {
JOptionPane.showMessageDialog(null, "除0错误!", "警告!",JOptionPane.ERROR_MESSAGE);
return 0;
}
sum = a / b;
break;
default:
return 0;
}
return sum;
}
}
package calculator;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.JTextField;
public class cal {
static String sum = "";
static String temp = "";
static int op = 0;
static JTextField tx1 ;
static JTextField tx2 ;
static JTextField tx3 ;
public static void main(String[] args) {
JFrame frame;
frame = new JFrame("calculator");
frame.setUndecorated(true); // 去掉窗口的装饰
frame.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);/*采用指定的窗口装饰 风格*/
frame.setBounds(300, 100,400,400 );
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(2,1));
JPanel p1 = new JPanel(new GridLayout(6,1));
JPanel p2 = new JPanel(new GridLayout(4,4));
tx1 = new JTextField();
tx2 = new JTextField();
tx3 = new JTextField();
tx1.setEditable(false);
tx2.setEditable(false);
tx3.setEditable(false);
p1.add(new JLabel(" 第一个数:"));
p1.add(tx1);
p1.add(new JLabel(" 第二个数:"));
p1.add(tx2);
p1.add(new JLabel(" 运算结果:"));
p1.add(tx3);
JButton b0 = new JButton("0");
JButton b1 = new JButton("1");
JButton b2 = new JButton("2");
JButton b3 = new JButton("3");
JButton b4 = new JButton("4");
JButton b5 = new JButton("5");
JButton b6 = new JButton("6");
JButton b7 = new JButton("7");
JButton b8 = new JButton("8");
JButton b9 = new JButton("9");
JButton b10 = new JButton("+");
JButton b11 = new JButton("-");
JButton b12 = new JButton("*");
JButton b13 = new JButton("/");
JButton b14 = new JButton("=");
JButton b15 = new JButton(".");
p2.add(b7);
p2.add(b8);
p2.add(b9);
p2.add(b10);
p2.add(b4);
p2.add(b5);
p2.add(b6);
p2.add(b11);
p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(b12);
p2.add(b15);
p2.add(b0);
p2.add(b14);
p2.add(b13);
frame.add(p1);
frame.add(p2);
frame.setVisible(true);
b0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
temp = "0";
sum = sum + temp;
if(op == 0)
tx1.setText(sum);
else tx2.setText(sum);
}
});
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
temp = "1";
sum = sum + temp;
if(op == 0)
tx1.setText(sum);
else tx2.setText(sum);
}
});
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
temp = "2";
sum = sum + temp;
if(op == 0)
tx1.setText(sum);
else tx2.setText(sum);
}
});
b3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
temp = "3";
sum = sum + temp;
if(op == 0)
tx1.setText(sum);
else tx2.setText(sum);
}
});
b4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
temp = "4";
sum = sum + temp;
if(op == 0)
tx1.setText(sum);
else tx2.setText(sum);
}
});
b5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
temp = "5";
sum = sum + temp;
if(op == 0)
tx1.setText(sum);
else tx2.setText(sum);
}
});
b6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
temp = "6";
sum = sum + temp;
if(op == 0)
tx1.setText(sum);
else tx2.setText(sum);
}
});
b7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
temp = "7";
sum = sum + temp;
if(op == 0)
tx1.setText(sum);
else tx2.setText(sum);
}
});
b8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
temp = "8";
sum = sum + temp;
if(op == 0)
tx1.setText(sum);
else tx2.setText(sum);
}
});
b9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
temp = "9";
sum = sum + temp;
if(op == 0)
tx1.setText(sum);
else tx2.setText(sum);
}
});
b15.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
temp = ".";
sum = sum + temp;
if(op == 0)
tx1.setText(sum);
else tx2.setText(sum);
}
});
b10.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
if(!tx1.getText().equalsIgnoreCase(""))
op = 1;
sum = "";
}
});
b11.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
if(!tx1.getText().equalsIgnoreCase(""))
op = 2;
sum = "";
}
});
b12.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
if(!tx1.getText().equalsIgnoreCase(""))
op = 3;
sum = "";
}
});
b13.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
if(!tx1.getText().equalsIgnoreCase(""))
op = 4;
sum = "";
}
});
b14.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
System.out.println(tx1.getText()+" "+op+" "+tx2.getText());
String s; double x;
if(!tx1.getText().equalsIgnoreCase("")&&!tx2.getText().equalsIgnoreCase("")
&& op != 0)
switch(op){
case 1:
x = Double.valueOf(tx1.getText())+Double.valueOf(tx2.getText());
System.out.println(Double.valueOf(tx1.getText()));
s = Double.toString(x);
tx3.setText(s);
sum = "";
op = 0;
break;
case 2:
x = Double.valueOf(tx1.getText())-Double.valueOf(tx2.getText());
s = Double.toString(x);
tx3.setText(s);
sum = "";
op = 0;
break;
case 3:
x = Double.valueOf(tx1.getText())*Double.valueOf(tx2.getText());
s = Double.toString(x);
tx3.setText(s);
sum = "";
op = 0;
break;
case 4:
x = Double.valueOf(tx1.getText())/Double.valueOf(tx2.getText());
s = Double.toString(x);
tx3.setText(s);
sum = "";
op = 0;
break;
}
}
});
}
}