java设计一个计算器界面的布局,具体的代码是什么
时间: 2024-10-23 11:09:20 浏览: 19
java实现计算器图形界面源代码(包含详细注释说明)
创建一个简单的Java Swing计算器界面通常涉及到JFrame、JPanel、JButton等组件的组合。这里是一个基本的步骤和示例代码,假设我们只关心加减乘除操作:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class SimpleCalculator {
private JFrame frame;
private JPanel panel;
private JTextField textField;
private JButton[] buttons;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
SimpleCalculator window = new SimpleCalculator();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public SimpleCalculator() {
initialize();
}
private void initialize() {
frame = new JFrame("简单计算器");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 400);
panel = new JPanel();
frame.add(panel);
// 创建文本框显示结果
textField = new JTextField();
textField.setEditable(false);
panel.add(textField, BorderLayout.NORTH);
// 创建数字按钮
int row = 4;
for (int i = 0; i <= 9; i++) {
JButton digit = new JButton(Integer.toString(i));
digit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + e.getActionCommand());
}
});
panel.add(digit, BorderLayout.CENTER);
if (i % 4 == 0 && i != 0) {
row++;
panel.add(Box.createVerticalStrut(10), BorderLayout.NORTH);
}
}
// 创建运算符按钮
buttons = new JButton[4];
buttons[0] = new JButton("+");
buttons[0].addActionListener(e -> operate("+", textField.getText()));
buttons[1] = new JButton("-");
buttons[1].addActionListener(e -> operate("-", textField.getText()));
buttons[2] = new JButton("*");
buttons[2].addActionListener(e -> operate("*", textField.getText()));
buttons[3] = new JButton("/");
buttons[3].addActionListener(e -> operate("/", textField.getText()));
for (int i = 0; i < 4; i++) {
panel.add(buttons[i], BorderLayout.NORTH);
}
// 添加清零按钮
JButton clear = new JButton("C");
clear.addActionListener(e -> textField.setText(""));
panel.add(clear, BorderLayout.WEST);
}
private void operate(String operator, String number) {
double result;
try {
result = Double.parseDouble(number) evalOperator(operator);
textField.setText(Double.toString(result));
} catch (NumberFormatException e) {
textField.setText("");
}
}
private double evalOperator(String op) {
return switch (op) {
case "+":
Double.parseDouble(textField.getText()) + Double.parseDouble(textField.getText());
case "-":
Double.parseDouble(textField.getText()) - Double.parseDouble(textField.getText());
case "*":
Double.parseDouble(textField.getText()) * Double.parseDouble(textField.getText());
case "/":
if (Double.parseDouble(textField.getText()) != 0)
Double.parseDouble(textField.getText()) / Double.parseDouble(textField.getText());
else
throw new ArithmeticException("除数不能为0");
default:
throw new IllegalArgumentException("不支持的操作符");
};
}
}
```
这个例子展示了一个基础的计算器界面,包含了数字按钮和几个基本的运算符。注意这只是一个非常简化的版本,实际应用可能需要处理更复杂的功能,比如错误处理、历史记录等。
阅读全文