import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Stack;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener {
private final GridBagLayout gbl = new GridBagLayout();//布局管理器
private final GridBagConstraints gbc = new GridBagConstraints();
String input = "";
private final JTextField result;
public Calculator() {
gbc.fill = GridBagConstraints.BOTH;
gbc.weighty = 1;
//结果文本组件
result = new JTextField();
result.setFont(new Font("宋体", Font.PLAIN, 20));
addComponent(this, result, 0, 0, 4, 1.0);//result
//顶部组件集合
JButton jButtonTemp;
JPanel topRow = new JPanel();
addComponent(this, topRow, 0, 1, 4, 1.0);
jButtonTemp = new JButton("C");
jButtonTemp.addActionListener(this);
addComponent(topRow, jButtonTemp, 0, 0, 1, 1.0);//JButton C
jButtonTemp = new JButton("%");
jButtonTemp.addActionListener(this);
addComponent(topRow, jButtonTemp, 1, 0, 1, 0.33);//JButton %
jButtonTemp = new JButton("+");
jButtonTemp.addActionListener(this);
addComponent(topRow, jButtonTemp, 2, 0, 1, 1.0);//JButton +
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++) {
jButtonTemp = new JButton(((2 - j) * 3 + i + 1) + "");
jButtonTemp.addActionListener(this);
addComponent(this, jButtonTemp, i, j + 2, 1, 1.0);
}
jButtonTemp = new JButton("-");
jButtonTemp.addActionListener(this);
addComponent(this, jButtonTemp, 3, 2, 1, 1.0);//JButton -
jButtonTemp = new JButton("x");
jButtonTemp.addActionListener(this);
addComponent(this, jButtonTemp, 3, 3, 1, 1.0);//JButton x
jButtonTemp = new JButton("/");
jButtonTemp.addActionListener(this);
addComponent(this, jButtonTemp, 3, 4, 1, 1.0);//JButton /
//底部组件集合
JPanel bottomRow = new JPanel();
addComponent(this, bottomRow, 0, 5, 4, 1.0);
jButtonTemp = new JButton("0");
jButtonTemp.addActionListener(this);
addComponent(bottomRow, jButtonTemp, 0, 0, 1, 1.0);//JButton 0
jButtonTemp = new JButton(".");
jButtonTemp.addActionListener(this);
addComponent(bottomRow, jButtonTemp, 1, 0, 1, 0.3);//JButton .
jButtonTemp = new JButton("=");
jButtonTemp.addActionListener(this);
addComponent(bottomRow, jButtonTemp, 2, 0, 1, 1.0);//JButton =
//属性设定
this.setTitle("加黎盾计算器");
this.setSize(600, 300);
this.setLocationRelativeTo(null);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//自定义组件添加布局方法
private void addComponent(Container con, Component comp, int gx, int gy, int gridwidth, double weightx) {
if (!(con.getLayout() instanceof GridBagLayout)) con.setLayout(gbl);
gbc.gridx = gx;
gbc.gridy = gy;
gbc.gridwidth = gridwidth;
gbc.weightx = weightx;
con.add(comp, gbc);
}
//监听器
@Override
public void actionPerformed(ActionEvent e) {
int cnt = 0;
String actionCommand = e.getActionCommand();
switch (actionCommand) {
case "+", "-", "x", "/", "%" -> input += " " + actionCommand + " ";
case "C" -> {
result.setText("");
input = "";
}
case "=" -> {
try {//当发现计算错误,即发现表达式不合法时,提示ERROR
input += " = " + calc(input);
result.setText(input);
} catch (Exception exception) {
result.setText("ERROR");
}
input = "";
cnt = 1;
}
default -> input += actionCommand;
}
if (cnt == 0)
result.setText(input);
}
//计算方法
private String calc(String input) {
String[] str;
str = input.split(" ");
Stack<Double> s = new Stack<Double>();
double m = Double.parseDouble(str[0]);
s.push(m);
for (int i = 1; i < str.length; i++) {
if (i % 2 == 1) {
if (str[i].compareTo("+") == 0) {
double help = Double.parseDouble(str[i + 1]);
s.push(help);
}
if (str[i].compareTo("-") == 0) {
double help = Double.parseDouble(str[i + 1]);
s.push(-help);
}
if (str[i].compareTo("x") == 0) {
double help = Double.parseDouble(str[i + 1]);
double ans = s.peek();//取栈顶
s.pop();//消栈
ans *= help;
s.push(ans);
}
if (str[i].compareTo("/") == 0) {
double help = Double.parseDouble(str[i + 1]);
double ans = s.peek();
s.pop();
ans /= help;
s.push(ans);
}
if (str[i].compareTo("%") == 0) {
double help = Double.parseDouble(str[i + 1]);
double ans = s.peek();
s.pop();
ans %= help;
s.push(ans);
}
}
}
double ans = 0;
while (!s.isEmpty()) {
ans += s.peek();
s.pop();
}
return String.valueOf(ans);
}
//主方法
public static void main(String[] args) {
new Calculator();
}
}
Java GUI计算器
最新推荐文章于 2022-06-30 22:36:58 发布