Java实现简易计算器

简述

  1. 使用JDK 1.8;
  2. 使用JFrame实现窗口,JFrame拥有比Applet更好的界面(个人认为);
  3. 已经实现基本的+、-、*、/四则运算,支持小数点,正负数,未实现除0判断;

程序截图

Java计算器

源代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Calculator extends JFrame {

private Container container;
//定义布局方式为网格包布局
private GridBagLayout layout;
private GridBagConstraints constraints;
//计算结果显示区
private JTextField displayField;
//保存+, -, *, /, =命令
private String lastCommand;
//保存计算结果
private double result;
//判断是否为数字的开始
private boolean start;

public Calculator() {
super("计算器");
setBounds(300, 200, 100, 100);
container = getContentPane();
layout = new GridBagLayout();
container.setLayout(layout);
constraints = new GridBagConstraints();
start = true;
result = 0;
//默认执行的操作
lastCommand = "=";
//定义计算区域
displayField = new JTextField(20);
//计算区域的数字靠右显示
displayField.setHorizontalAlignment(JTextField.RIGHT);
//定义计算区域的背景颜色
displayField.setBackground(Color.white);
//定义计算区域的前景颜色
displayField.setForeground(Color.blue);
displayField.setFont(new Font("Arial", 24, 24));
//定义计算区域的X,Y轴
constraints.gridx = 0;
constraints.gridy = 0;
//定义计算区域的宽度和高度
constraints.gridwidth = 4;
constraints.gridheight = 1;
//定义控件填充满剩余空间
constraints.fill = GridBagConstraints.BOTH;
//定义计算区域的X,Y轴单位度量值
constraints.weightx = 50;
constraints.weighty = 50;
layout.setConstraints(displayField, constraints);
//将计算区域加入到窗口中
container.add(displayField);
//定义两个监视器
ActionListener insert = new InsertAction();
ActionListener command = new CommandAction();
//通过定义好的方法添加按钮
addButton("Bck", 0, 1, 1, 1, insert);
addButton("CE", 1, 1, 1, 1, insert);
addButton("CLEAR", 2, 1, 2, 1, insert);
addButton("7", 0, 2, 1, 1, insert);
addButton("8", 1, 2, 1, 1, insert);
addButton("9", 2, 2, 1, 1, insert);
addButton("/", 3, 2, 1, 1, command);
addButton("4", 0, 3, 1, 1, insert);
addButton("5", 1, 3, 1, 1, insert);
addButton("6", 2, 3, 1, 1, insert);
addButton("*", 3, 3, 1, 1, command);
addButton("1", 0, 4, 1, 1, insert);
addButton("2", 1, 4, 1, 1, insert);
addButton("3", 2, 4, 1, 1, insert);
addButton("-", 3, 4, 1, 1, command);
addButton("0", 0, 5, 1, 1, insert);
//只显示"-"号, "+"不显示
addButton("+/-", 1, 5, 1, 1, insert);
addButton(".", 2, 5, 1, 1, insert);
addButton("+", 3, 5, 1, 1, command);
addButton("Powered By GYSHGX868", 0, 6, 3, 1, insert);
addButton("=", 3, 6, 1, 1, command);
//设置窗口的大小
setSize(300, 300);
//设置窗口可见
setVisible(true);
}

private void addButton(String label, int row, int column, int with,
int height, ActionListener listener) {
// 定义添加按钮的方法
JButton button = new JButton(label);
constraints.gridx = row;
constraints.gridy = column;
constraints.gridwidth = with;
constraints.gridheight = height;
constraints.fill = GridBagConstraints.BOTH;
button.addActionListener(listener);
layout.setConstraints(button, constraints);
container.add(button);
//设置按钮间的间距
constraints.insets = new Insets(1, 1, 1, 1);
//设置字体
button.setFont(new Font("Arial", 12, 18));
button.setForeground(Color.MAGENTA);
button.setBorderPainted(true);
button.setContentAreaFilled(true);
button.setBounds(2, 2, 2, 2);
button.contains(5, 5);
}

private class InsertAction implements ActionListener {
//定义Insert事件的方法
public void actionPerformed(ActionEvent event) {

String input = event.getActionCommand();
if (start) {
displayField.setText(""); //默认计算区域为空
start = false;
//如果开始输入的是"+/-",以负数显示
if (input.equals("+/-"))
displayField.setText(displayField.getText() + "-");
}

if (!input.equals("+/-")) {
if (input.equals("Bck")) {
String str = displayField.getText();
if (str.length() > 0) {
//如果单击退格,实现退格的功能
displayField.setText(str.substring(0, str.length() - 1));
}
} else if (input.equals("CE") || input.equals("CLEAR")) {
displayField.setText("0");
start = true; //实现清零和复位的功能
} else if (input.equals(".")) {
if (displayField.getText().trim().indexOf(".") == -1) {
//输入小数点时,判断是否已经输入过小数点
displayField.setText(displayField.getText() + input);
}
} else
displayField.setText(displayField.getText() + input);
}
}
}

private class CommandAction implements ActionListener {

public void actionPerformed(ActionEvent evt) {
String command = evt.getActionCommand();
if (start) {
lastCommand = command;
} else {
calculate(Double.parseDouble(displayField.getText()));
lastCommand = command;
start = true; //记忆输入的操作符号
}
}
}

public void calculate(double x) {
if (lastCommand.equals("+"))
result += x;
else if (lastCommand.equals("-"))
result -= x;
else if (lastCommand.equals("*"))
result *= x;
else if (lastCommand.equals("/"))
result /= x;
else if (lastCommand.equals("="))
result = x; //实现计算的功能
//显示计算的结果
displayField.setText("" + result);
}

public static void main(String[] args) {
//初始化计算器
Calculator calculator = new Calculator();
calculator.setResizable(false);
calculator.setForeground(Color.MAGENTA);
//关闭窗口,退出程序
calculator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}