package gui;
import java.awt.Container;
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.JPanel;
import javax.swing.JTextField;
public class Caculator extends JFrame {
public static void main(String[] args) {
new Caculator();
}
// 构造方法
public Caculator() {
Container container = getContentPane();// 定义一个顶级容器
setLayout(new GridLayout(2, 1));
final JTextField jTextField = new JTextField();
jTextField.setHorizontalAlignment(jTextField.RIGHT);
JButton jButton0 = new JButton("0");
JButton jButton1 = new JButton("1");
JButton jButton2 = new JButton("2");
JButton jButton3 = new JButton("3");
JButton jButton4 = new JButton("4");
JButton jButton5 = new JButton("5");
JButton jButton6 = new JButton("6");
JButton jButton7 = new JButton("7");
JButton jButton8 = new JButton("8");
JButton jButton9 = new JButton("9");
JButton point = new JButton(".");
JButton equ = new JButton("=");
JButton plus = new JButton("+");
JButton minus = new JButton("-");
JButton mtp = new JButton("*");
JButton dvd = new JButton("/");
JButton sqr = new JButton("sqrt");
JButton root = new JButton("x^2");
JButton tg = new JButton("退格");
JButton ql = new JButton("清零");
JPanel jPanel = new JPanel();
jPanel.setLayout(new GridLayout(4, 5, 3, 3));// 3,3为水平垂直间距
jPanel.add(jButton7);
jPanel.add(jButton8);
jPanel.add(jButton9);
jPanel.add(plus);
jPanel.add(sqr);
jPanel.add(jButton4);
jPanel.add(jButton5);
jPanel.add(jButton6);
jPanel.add(minus);
jPanel.add(root);
jPanel.add(jButton1);
jPanel.add(jButton2);
jPanel.add(jButton3);
jPanel.add(mtp);
jPanel.add(ql);
jPanel.add(jButton0);
jPanel.add(point);
jPanel.add(equ);
jPanel.add(dvd);
jPanel.add(tg);
container.add(jTextField);
container.add(jPanel);
setSize(400, 300);
setVisible(true);
setTitle("计算器");
setDefaultCloseOperation(EXIT_ON_CLOSE);
// 计算器功能实现
jButton0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (jTextField.getText().equals("0")) {// 与0作比较
jTextField.requestFocus();// 把输入焦点放在调用这个方法的控件上
} else {
String string = jTextField.getText();
jTextField.setText(string + "0");
}
}
});
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (jTextField.getText().equals("0")) {
jTextField.setText("");
jTextField.setText("1");
jTextField.requestFocus();
} else {
String string = jTextField.getText();
jTextField.setText(string + "1");
}
}
});
jButton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (jTextField.getText().equals("0")) {
jTextField.setText("");
jTextField.setText("2");
jTextField.requestFocus();
} else {
String string = jTextField.getText();
jTextField.setText(string + "2");
}
}
});
jButton3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (jTextField.getText().equals("0")) {
jTextField.setText("");
jTextField.setText("3");
jTextField.requestFocus();
} else {
String string = jTextField.getText();
jTextField.setText(string + "3");
}
}
});
jButton4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (jTextField.getText().equals("0")) {
jTextField.setText("");
jTextField.setText("4");
jTextField.requestFocus();
} else {
String string = jTextField.getText();
jTextField.setText(string + "4");
}
}
});
jButton5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (jTextField.getText().equals("0")) {
jTextField.setText("");
jTextField.setText("5");
jTextField.requestFocus();
} else {
String string = jTextField.getText();
jTextField.setText(string + "5");
}
}
});
jButton6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (jTextField.getText().equals("0")) {
jTextField.setText("");
jTextField.setText("6");
jTextField.requestFocus();
} else {
String string = jTextField.getText();
jTextField.setText(string + "6");
}
}
});
jButton7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (jTextField.getText().equals("0")) {
jTextField.setText("");
jTextField.setText("7");
jTextField.requestFocus();
} else {
String string = jTextField.getText();
jTextField.setText(string + "7");
}
}
});
jButton8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (jTextField.getText().equals("0")) {
jTextField.setText("");
jTextField.setText("8");
jTextField.requestFocus();
} else {
String string = jTextField.getText();
jTextField.setText(string + "8");
}
}
});
jButton9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (jTextField.getText().equals("0")) {
jTextField.setText("");
jTextField.setText("9");
jTextField.requestFocus();
} else {
String string = jTextField.getText();
jTextField.setText(string + "9");
}
}
});
point.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (jTextField.getText().equals("0")) {
jTextField.setText("");
jTextField.setText(".");
jTextField.requestFocus();
} else {
String string = jTextField.getText();
jTextField.setText(string + ".");
}
}
});
plus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (jTextField.getText().equals("0")) {
jTextField.setText("");
jTextField.setText("+");
jTextField.requestFocus();
} else {
String string = jTextField.getText();
jTextField.setText(string + "+");
}
}
});
minus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (jTextField.getText().equals("0")) {
jTextField.setText("");
jTextField.setText("-");
jTextField.requestFocus();
} else {
String string = jTextField.getText();
jTextField.setText(string + "-");
}
}
});
mtp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (jTextField.getText().equals("0")) {
jTextField.setText("");
jTextField.setText("*");
jTextField.requestFocus();
} else {
String string = jTextField.getText();
jTextField.setText(string + "*");
}
}
});
dvd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (jTextField.getText().equals("0")) {
jTextField.setText("");
jTextField.setText("/");
jTextField.requestFocus();
} else {
String string = jTextField.getText();
jTextField.setText(string + "/");
}
}
});
tg.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String string = jTextField.getText();
int length = string.length();
if (length > 0)
string = string.substring(0, length - 1);
if (string.length() == 0)
jTextField.setText("0");
else
jTextField.setText(string);
}
});
ql.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
jTextField.setText("0");
}
});
root.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String string = jTextField.getText();
Double double1 = Double.parseDouble(string);// 将字符串转换为double
Double double2 = double1 * double1;
String string2 = string.valueOf(double2);// 将double转换为string
jTextField.setText(string2);
}
});
sqr.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String string = jTextField.getText();
Double double1 = Double.parseDouble(string);// 将字符串转换为double
Double double2 = (Double) Math.sqrt(double1);
String string2 = string.valueOf(double2);// 将double转换为string
jTextField.setText(string2);
}
});
equ.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (jTextField.getText().indexOf("+") != -1) {
String[] string = jTextField.getText().split("[+]");// ******转义字符*****
Double double1 = Double.parseDouble(string[0]);
Double double2 = Double.parseDouble(string[1]);
Double double3 = double1 + double2;
String string2 = String.valueOf(double3);
jTextField.setText(string2);
} else if (jTextField.getText().indexOf("-") != -1) {
String[] string = jTextField.getText().split("-");
Double double1 = Double.parseDouble(string[0]);
Double double2 = Double.parseDouble(string[1]);
Double double3 = double1 - double2;
String string2 = String.valueOf(double3);
jTextField.setText(string2);
} else if (jTextField.getText().indexOf("*") != -1) {
String[] string = jTextField.getText().split("[*]");// ***转义字符***
Double double1 = Double.parseDouble(string[0]);
Double double2 = Double.parseDouble(string[1]);
Double double3 = double1 * double2;
String string2 = String.valueOf(double3);
jTextField.setText(string2);
} else if (jTextField.getText().indexOf("/") != -1) {
String[] string = jTextField.getText().split("/");
Double double1 = Double.parseDouble(string[0]);
Double double2 = Double.parseDouble(string[1]);
Double double3 = double1 / double2;
String string2 = String.valueOf(double3);
jTextField.setText(string2);
} else {
jTextField.setText("还没有你所输入的用算");
}
}
});
}
}
转载于:https://my.oschina.net/u/3778090/blog/1838109