java简易计算器界面设计
时间: 2024-01-02 18:22:55 浏览: 96
简单的java计算器界面
4星 · 用户满意度95%
以下是一个简易计算器的Java界面设计示例:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class CalculatorGUI extends JFrame {
private JTextField displayField;
private JButton[] numberButtons;
private JButton[] operatorButtons;
private JButton equalsButton;
private JButton clearButton;
public CalculatorGUI() {
setTitle("简易计算器");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
displayField = new JTextField();
displayField.setEditable(false);
displayField.setHorizontalAlignment(JTextField.RIGHT);
numberButtons = new JButton[10];
for (int i = 0; i < 10; i++) {
numberButtons[i] = new JButton(String.valueOf(i));
}
operatorButtons = new JButton[4];
operatorButtons[0] = new JButton("+");
operatorButtons[1] = new JButton("-");
operatorButtons[2] = new JButton("*");
operatorButtons[3] = new JButton("/");
equalsButton = new JButton("=");
clearButton = new JButton("CE");
JPanel buttonPanel = new JPanel(new GridLayout(4, 4));
buttonPanel.add(numberButtons[7]);
buttonPanel.add(numberButtons[8]);
buttonPanel.add(numberButtons[9]);
buttonPanel.add(operatorButtons[0]);
buttonPanel.add(numberButtons[4]);
buttonPanel.add(numberButtons[5]);
buttonPanel.add(numberButtons[6]);
buttonPanel.add(operatorButtons[1]);
buttonPanel.add(numberButtons[1]);
buttonPanel.add(numberButtons[2]);
buttonPanel.add(numberButtons[3]);
buttonPanel.add(operatorButtons[2]);
buttonPanel.add(numberButtons[0]);
buttonPanel.add(clearButton);
buttonPanel.add(equalsButton);
buttonPanel.add(operatorButtons[3]);
setLayout(new BorderLayout());
add(displayField, BorderLayout.NORTH);
add(buttonPanel, BorderLayout.CENTER);
pack();
setLocationRelativeTo(null);
// 添加事件监听器
for (int i = 0; i < 10; i++) {
final int num = i;
numberButtons[i].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
displayField.setText(displayField.getText() + num);
}
});
}
for (int i = 0; i < 4; i++) {
final String operator = operatorButtons[i].getText();
operatorButtons[i].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
displayField.setText(displayField.getText() + " " + operator + " ");
}
});
}
equalsButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String expression = displayField.getText();
String[] parts = expression.split(" ");
double result = Double.parseDouble(parts[0]);
for (int i = 1; i < parts.length; i += 2) {
String operator = parts[i];
double operand = Double.parseDouble(parts[i + 1]);
switch (operator) {
case "+":
result += operand;
break;
case "-":
result -= operand;
break;
case "*":
result *= operand;
break;
case "/":
result /= operand;
break;
}
}
displayField.setText(String.valueOf(result));
}
});
clearButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
displayField.setText("");
}
});
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new CalculatorGUI().setVisible(true);
}
});
}
}
```
阅读全文