界面如下:
实现代码:
package 简易计算器;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Stack;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Calculator {
JFrame frame = new JFrame("计算器");
// 运算符栈和操作数栈
Stack OPTR = new Stack();
Stack OPND = new Stack();
// 北面组件
JPanel pNorth = new JPanel();
JTextField inputTextField = new JTextField();// 输入文本框
JButton clearButton = new JButton("C");// 清除按钮
// 中间组件,设置为网格布局
JPanel pCenter = new JPanel(new GridLayout(4, 4, 4, 4));
// 相关成员变量
String opndS;// 操作数字符串
double opndD;// 操作数
String operator = null;// 运算符
String text = null;// 文本框内容
double result;// 计算结果
int index = -1;// 字符串截取下标
// 比较运算符优先级
public String Precede(String optr1, String optr2) {
switch (optr1) {
case "+":
if (optr2.equals("*") || optr2.equals("/"))
return "<";
else
return ">";
case "-":
if (optr2.equals("*") || optr2.equals("/"))
return "<";
else
return ">";
case "*":
return ">";
case "/":
return ">";
case "=":
if (optr2.equals("="))
return "=";
else
return "<";
default:
return "";
}
}
// 操作数进行运算
public double calculate(double a, String optr, double b) {
switch (optr) {
case "+":
return a + b;
case "-":
return a - b;
case "*":
return a * b;
case "/":
return a / b;
default:
return 0;
}
}
// 界面初始化
public void init() {
OPTR.push("=");
// 窗口设置
frame.setSize(252, 333);// 设置大小
frame.setLocationRelativeTo(null);// 设置窗口居中显示
frame.setResizable(false);// 不可更改大小
frame.setVisible(true);// 窗口可见
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 退出窗口
// 北面设置
inputTextField.setPreferredSize(new Dimension(172, 53));// 文本框大小
inputTextField.setEditable(false);// 设置为不可编辑
inputTextField.setFont(new Font("粗体", Font.BOLD, 15));// 字体大小及风格
inputTextField.setHorizontalAlignment(JTextField.RIGHT);// 文本设置为靠右边显示
clearButton.setForeground(Color.red);// 清除按钮的颜色为红色
clearButton.setPreferredSize(new Dimension(56, 53));// 设置按钮大小
// 为清除按钮注册监听
clearButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
inputTextField.setText("");
}
});
pNorth.add(inputTextField);// 将输入文本框添加进北部容器
pNorth.add(clearButton);// 将清除按钮添加进北部容器
frame.add(pNorth, BorderLayout.NORTH);// 将北部容器添加进窗口frame中
// 中间设置
String btName = "123+456-789*0.=/";// 按钮名称
String regex = "[\+\-*/.=]";// 正则表达式
for (int i = 0; i < 16; i++) {
String name = btName.substring(i, i + 1);// 从btName中截取字符
JButton bt = new JButton();
bt.setText(name);
// 设置运算符按钮的大小,颜色为蓝色
if (name.matches(regex)) {
bt.setForeground(Color.blue);
bt.setFont(new Font("粗体", Font.BOLD, 14));
} else {// 设置数字按钮的大小,格式为斜体
bt.setFont(new Font("粗体", Font.ITALIC, 14));
}
// 为这些按钮注册监听
bt.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String click = e.getActionCommand();// 获取鼠标点击按钮的字符串
// 如果点击运算符按钮
if (click.matches("[\+\-*/=]")) {
String input = inputTextField.getText();// 当前文本框内容
opndS = input.substring(index + 1);// 当前运算符前的操作数字符串
opndD = Double.valueOf(opndS);// 将字符串转换为double类型数字
OPND.push(opndD);// 将操作数入栈
// 比较运算符栈的栈顶运算符与当前点击的运算符优先级大小
switch (Precede(OPTR.peek(), click)) {
case ">":
// 如果栈顶运算符优先级大于click,循环执行出栈->入栈-出栈->入栈操作
while (Precede(OPTR.peek(), click).equals(">")) {
// 弹出两个操作数
double b = OPND.pop();
double a = OPND.pop();
// 弹出运算符
String optr = OPTR.pop();
double temp = calculate(a, optr, b);
// 将计算结果入栈
OPND.push(temp);
}
if (click.equals("="))
result = OPND.pop();
else
OPTR.push(click);
break;
case "<":
OPTR.push(click);// 栈顶优先级小于click,将其入栈
break;
}
if (click.equals("=")) {
index = -1;
inputTextField.setText(result + "");// 显示计算结果
text = inputTextField.getText();// 将文本框内容标记为计算结果
} else {
index = input.length();// 当前文本框内容长度
inputTextField.setText(inputTextField.getText() + e.getActionCommand());// 设置文本框内容
text = null;// 文本框内容标记为空
}
// 如果文本框内容不为空 并且点击的是数字按钮,将对文本框作清空操作
} else if (text != null && !click.matches("[\+\-*/]")) {
inputTextField.setText("");
inputTextField.setText(inputTextField.getText() + e.getActionCommand());
text = null;
} else {
inputTextField.setText(inputTextField.getText() + e.getActionCommand());
}
}
});
pCenter.add(bt);// 将16个按钮添加进中间容器
}
frame.add(pCenter);// 将中间容器添加进窗口frame中
}
public static void main(String[] args) {
new Calculator().init();
}
}