Swing、MFC都是过时框架了吧,大学还这么喜欢用
现在手机上没办法调试,用claude试了一下
package MYM;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Stack;
public class MYM {
private JFrame frame;
private JTextField textField;
private String currentInput = "";
private boolean lastWasOperator = false;
public MYM() {
frame = new JFrame("Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 500);
frame.setLayout(new BorderLayout());
textField = new JTextField();
textField.setFont(new Font("Arial", Font.PLAIN, 24));
textField.setEditable(false);
textField.setHorizontalAlignment(JTextField.RIGHT);
frame.add(textField, BorderLayout.NORTH);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(5, 4, 5, 5)); // Added gaps between buttons
String[] buttons = {
"7", "8", "9", "/",
"4", "5", "6", "*",
"1", "2", "3", "-",
"0", ".", "=", "+",
"C", "←"
};
for (String label : buttons) {
JButton button = new JButton(label);
button.setFont(new Font("Arial", Font.PLAIN, 24));
button.addActionListener(new ButtonClickListener());
// Style the buttons
if (label.matches("[0-9.]")) {
button.setBackground(new Color(240, 240, 240));
} else if (label.equals("=")) {
button.setBackground(new Color(100, 149, 237));
button.setForeground(Color.WHITE);
} else if (label.equals("C") || label.equals("←")) {
button.setBackground(new Color(255, 160, 122));
} else {
button.setBackground(new Color(220, 220, 220));
}
button.setFocusPainted(false);
panel.add(button);
}
// Add empty buttons to fill the last row
for (int i = 0; i < 2; i++) {
JButton emptyButton = new JButton();
emptyButton.setEnabled(false);
emptyButton.setBackground(panel.getBackground());
panel.add(emptyButton);
}
panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
frame.add(panel, BorderLayout.CENTER);
frame.setVisible(true);
}
private class ButtonClickListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.equals("=")) {
if (!currentInput.isEmpty() && !lastWasOperator) {
try {
currentInput = evaluateExpression(currentInput);
textField.setText(currentInput);
} catch (Exception ex) {
textField.setText("Error");
currentInput = "";
}
}
} else if (command.equals("C")) {
currentInput = "";
textField.setText(currentInput);
lastWasOperator = false;
} else if (command.equals("←")) {
if (!currentInput.isEmpty()) {
currentInput = currentInput.substring(0, currentInput.length() - 1);
if (!currentInput.isEmpty()) {
lastWasOperator = isOperator(currentInput.charAt(currentInput.length() - 1));
} else {
lastWasOperator = false;
}
textField.setText(currentInput);
}
} else if (isOperator(command.charAt(0))) {
if (!currentInput.isEmpty() && !lastWasOperator) {
currentInput += command;
textField.setText(currentInput);
lastWasOperator = true;
}
} else {
if (command.equals(".") && (currentInput.isEmpty() || lastWasOperator)) {
currentInput += "0";
}
currentInput += command;
textField.setText(currentInput);
lastWasOperator = false;
}
}
}
private boolean isOperator(char c) {
return c == '+' || c == '-' || c == '*' || c == '/';
}
private String evaluateExpression(String expression) {
try {
double result = evaluate(expression);
// Format the result to remove unnecessary decimal places
if (result == (long) result) {
return String.format("%d", (long) result);
} else {
return String.format("%.8f", result).replaceAll("0*$", "").replaceAll("\\.$", "");
}
} catch (Exception e) {
return "Error";
}
}
private double evaluate(String expression) {
Stack<Double> numbers = new Stack<>();
Stack<Character> operators = new Stack<>();
for (int i = 0; i < expression.length(); i++) {
char c = expression.charAt(i);
if (Character.isDigit(c) || c == '.') {
StringBuilder num = new StringBuilder();
while (i < expression.length() && (Character.isDigit(expression.charAt(i)) || expression.charAt(i) == '.')) {
num.append(expression.charAt(i));
i++;
}
i--;
numbers.push(Double.parseDouble(num.toString()));
} else if (isOperator(c)) {
while (!operators.empty() && hasPrecedence(c, operators.peek())) {
numbers.push(applyOperation(operators.pop(), numbers.pop(), numbers.pop()));
}
operators.push(c);
}
}
while (!operators.empty()) {
numbers.push(applyOperation(operators.pop(), numbers.pop(), numbers.pop()));
}
return numbers.pop();
}
private boolean hasPrecedence(char op1, char op2) {
if ((op1 == '*' || op1 == '/') && (op2 == '+' || op2 == '-')) return false;
return true;
}
private double applyOperation(char operator, double b, double a) {
switch (operator) {
case '+': return a + b;
case '-': return a - b;
case '*': return a * b;
case '/':
if (b == 0) throw new ArithmeticException("Division by zero");
return a / b;
}
return 0;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new MYM());
}
}
1 Like
try-with-resources 语句,主要做AutoCloseable
多打印日志,debug运行调试
swing? 6
此话题已在最后回复的 30 天后被自动关闭。不再允许新回复。