package item;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Calculator {
static JTextField textField;
static int num1 = 0;
static int num2 = 0;
public static void main(String[] args) {
// 网格布局
Font font = new Font("黑体", Font.BOLD, 32);
UIManager.put("Button.font", font);
JFrame frame = new JFrame("计算器");
textField = new JTextField(24);// 文本最大输入长度。
textField.setPreferredSize(new Dimension(12, 66));// --可以使用Dimension方法来设置布局内部组件地大小。
textField.setFont(new Font("宋体", Font.PLAIN, 24));
JButton button = new JButton("1");
JButton button1 = new JButton("2");
JButton button2 = new JButton("3");
JButton button3 = new JButton("+");
JButton button4 = new JButton("4");
JButton button5 = new JButton("5");
JButton button6 = new JButton("6");
JButton button7 = new JButton("-");
JButton button8 = new JButton("7");
JButton button9 = new JButton("8");
JButton button10 = new JButton("9");
JButton button11 = new JButton("x");
JButton button12 = new JButton("c");
JButton button13 = new JButton("0");
JButton button14 = new JButton("/");
JButton button15 = new JButton("=");
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(4, 4, 4, 4));
// -----------------------事件-------------------------
ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String content = e.getActionCommand();
String message = textField.getText();
switch (content) {
case "1":
message = message + 1;
textField.setText(message);
break;
case "2":
message = message + 2;
textField.setText(message);
break;
case "3":
message = message + 3;
textField.setText(message);
break;
case "4":
message = message + 4;
textField.setText(message);
break;
case "5":
message = message + 5;
textField.setText(message);
break;
case "6":
message = message + 6;
textField.setText(message);
break;
case "7":
message = message + 7;
textField.setText(message);
break;
case "8":
message = message + 8;
textField.setText(message);
break;
case "9":
message = message + 9;
textField.setText(message);
break;
case "0":
message = message + 0;
textField.setText(message);
break;
case "c":
message = "";
textField.setText(message);
break;
case "+":
message = message + "+";
textField.setText(message);
break;
case "-":
message = message + "-";
textField.setText(message);
break;
case "x":
message = message + "x";
textField.setText(message);
break;
case "/":
message = message + "/";
textField.setText(message);
break;
case "=":
int a = message.indexOf("+");
int b = message.indexOf("-");
int c = message.indexOf("x");
int d = message.indexOf("/");
if (b == -1) {
if (a != -1) {
String[] strings = message.split("\\+", 2);
String s1 = strings[0];
String s2 = strings[1];
num1 = Integer.parseInt(s1);
num2 = Integer.parseInt(s2);
message = (num1 + num2) + "";
textField.setText(message);
}
if (c != -1) {
String[] strings = message.split("x", 2);
String s1 = strings[0];
String s2 = strings[1];
num1 = Integer.parseInt(s1);
num2 = Integer.parseInt(s2);
message = (num1 * num2) + "";
textField.setText(message);
}
if (d != -1) {
String[] strings = message.split("\\/", 2);
String s1 = strings[0];
String s2 = strings[1];
num1 = Integer.parseInt(s1);
num2 = Integer.parseInt(s2);
message = (1.0 * num1 / num2) + "";
textField.setText(message);
}
}
if (b != -1) {
if (a != -1) {
String[] strings = message.split("\\+", 2);
String s1 = strings[0];
String s2 = strings[1];
num1 = Integer.parseInt(s1);
num2 = Integer.parseInt(s2);
message = (num1 + num2) + "";
textField.setText(message);
}
if (c != -1) {
String[] strings = message.split("x", 2);
String s1 = strings[0];
String s2 = strings[1];
num1 = Integer.parseInt(s1);
num2 = Integer.parseInt(s2);
message = (num1 * num2) + "";
textField.setText(message);
}
if (d != -1) {
String[] strings = message.split("\\/", 2);
String s1 = strings[0];
String s2 = strings[1];
num1 = Integer.parseInt(s1);
num2 = Integer.parseInt(s2);
message = (1.0 * num1 / num2) + "";
textField.setText(message);
}
if (b == 0 && a == -1 && c == -1 && d == -1) {
String[] strings = message.split("\\-", 3);
String s1 = strings[1];
String s2 = strings[2];
num1 = Integer.parseInt(s1);
num2 = Integer.parseInt(s2);
message = (-(num1 + num2)) + "";
textField.setText(message);
}
if (b > 0 && a == -1 && c == -1 && d == -1) {
String[] strings = message.split("\\-", 2);
String s1 = strings[0];
String s2 = strings[1];
num1 = Integer.parseInt(s1);
num2 = Integer.parseInt(s2);
message = (num1 - num2) + "";
textField.setText(message);
}
}
break;
default:
}
}
};
// ----------
button.addActionListener(actionListener);
button1.addActionListener(actionListener);
button2.addActionListener(actionListener);
button3.addActionListener(actionListener);
button4.addActionListener(actionListener);
button5.addActionListener(actionListener);
button6.addActionListener(actionListener);
button7.addActionListener(actionListener);
button8.addActionListener(actionListener);
button9.addActionListener(actionListener);
button10.addActionListener(actionListener);
button11.addActionListener(actionListener);
button12.addActionListener(actionListener);
button13.addActionListener(actionListener);
button14.addActionListener(actionListener);
button15.addActionListener(actionListener);
// --------------------------------------------------
// frame.add(button);
panel.add(button);
panel.add(button1);
panel.add(button2);
panel.add(button3);
panel.add(button4);
panel.add(button5);
panel.add(button6);
panel.add(button7);
panel.add(button8);
panel.add(button9);
panel.add(button10);
panel.add(button11);
panel.add(button12);
panel.add(button13);
panel.add(button14);
panel.add(button15);
frame.add(textField, BorderLayout.NORTH);
frame.add(panel, BorderLayout.CENTER);
// frame.pack();
frame.setBounds(400, 400, 280, 340);
frame.setResizable(false);// 大小不可变
frame.setLocationRelativeTo(null);// 设置frame总位于屏幕中心。
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 退出将关闭程序。
}
}