计算器静态界面设计java代码
时间: 2023-06-23 08:59:04 浏览: 89
以下是一个简单的计算器静态界面的Java代码:
```java
import javax.swing.*;
import java.awt.*;
public class CalculatorGUI extends JFrame {
private JPanel mainPanel;
private JTextField inputField;
private JButton button0;
private JButton button1;
private JButton button2;
private JButton button3;
private JButton button4;
private JButton button5;
private JButton button6;
private JButton button7;
private JButton button8;
private JButton button9;
private JButton buttonAdd;
private JButton buttonSubtract;
private JButton buttonMultiply;
private JButton buttonDivide;
private JButton buttonEquals;
private JButton buttonClear;
public CalculatorGUI() {
setTitle("Calculator");
setSize(300, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
mainPanel = new JPanel(new BorderLayout());
inputField = new JTextField();
inputField.setHorizontalAlignment(JTextField.RIGHT);
inputField.setPreferredSize(new Dimension(300, 50));
JPanel buttonPanel = new JPanel(new GridLayout(4, 4));
button0 = new JButton("0");
button1 = new JButton("1");
button2 = new JButton("2");
button3 = new JButton("3");
button4 = new JButton("4");
button5 = new JButton("5");
button6 = new JButton("6");
button7 = new JButton("7");
button8 = new JButton("8");
button9 = new JButton("9");
buttonAdd = new JButton("+");
buttonSubtract = new JButton("-");
buttonMultiply = new JButton("*");
buttonDivide = new JButton("/");
buttonEquals = new JButton("=");
buttonClear = new JButton("C");
buttonPanel.add(button7);
buttonPanel.add(button8);
buttonPanel.add(button9);
buttonPanel.add(buttonAdd);
buttonPanel.add(button4);
buttonPanel.add(button5);
buttonPanel.add(button6);
buttonPanel.add(buttonSubtract);
buttonPanel.add(button1);
buttonPanel.add(button2);
buttonPanel.add(button3);
buttonPanel.add(buttonMultiply);
buttonPanel.add(buttonClear);
buttonPanel.add(button0);
buttonPanel.add(buttonEquals);
buttonPanel.add(buttonDivide);
mainPanel.add(inputField, BorderLayout.NORTH);
mainPanel.add(buttonPanel, BorderLayout.CENTER);
setContentPane(mainPanel);
setVisible(true);
}
public static void main(String[] args) {
new CalculatorGUI();
}
}
```
上述代码使用了Java Swing GUI库来创建计算器的静态界面。界面包括一个文本框和各种按钮,通过布局管理器将它们放置在合适的位置。用户可以通过按钮输入数字和运算符,计算器会在文本框中显示计算结果。
阅读全文