用java代码写一个计算器静态界面设计
时间: 2023-11-19 08:04:26 浏览: 71
计算器界面java设计
好的,以下是一个简单的 Java Swing 计算器的静态界面设计代码示例:
```java
import javax.swing.*;
import java.awt.*;
public class Calculator {
public static void main(String[] args) {
JFrame frame = new JFrame("Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 400);
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 4;
JTextField textField = new JTextField();
textField.setEditable(false);
panel.add(textField, c);
c.gridwidth = 1;
c.gridy = 1;
panel.add(new JButton("7"), c);
c.gridx = 1;
panel.add(new JButton("8"), c);
c.gridx = 2;
panel.add(new JButton("9"), c);
c.gridx = 3;
panel.add(new JButton("/"), c);
c.gridx = 0;
c.gridy = 2;
panel.add(new JButton("4"), c);
c.gridx = 1;
panel.add(new JButton("5"), c);
c.gridx = 2;
panel.add(new JButton("6"), c);
c.gridx = 3;
panel.add(new JButton("*"), c);
c.gridx = 0;
c.gridy = 3;
panel.add(new JButton("1"), c);
c.gridx = 1;
panel.add(new JButton("2"), c);
c.gridx = 2;
panel.add(new JButton("3"), c);
c.gridx = 3;
panel.add(new JButton("-"), c);
c.gridx = 0;
c.gridy = 4;
panel.add(new JButton("0"), c);
c.gridx = 1;
panel.add(new JButton("."), c);
c.gridx = 2;
panel.add(new JButton("="), c);
c.gridx = 3;
panel.add(new JButton("+"), c);
frame.add(panel);
frame.setVisible(true);
}
}
```
这个代码示例使用了 Java Swing 的组件来构建计算器的界面,使用了 GridBagLayout 网格布局来排列按钮。您可以根据需要进行修改和扩展。
阅读全文