用java实现Windows10的计算器界面
时间: 2024-03-04 19:48:09 浏览: 94
实现Windows10的计算器界面需要使用Java GUI库,如Swing或JavaFX。以下是一个简单的示例程序:
```java
import javax.swing.*;
import java.awt.*;
public class Calculator extends JFrame {
private JPanel panel;
private JTextField textField;
private JButton button0, button1, button2, button3, button4, button5, button6, button7, button8, button9;
private JButton buttonAdd, buttonSub, buttonMul, buttonDiv, buttonEqual, buttonClear;
public Calculator() {
super("Calculator");
textField = new JTextField(20);
textField.setEditable(false);
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("+");
buttonSub = new JButton("-");
buttonMul = new JButton("*");
buttonDiv = new JButton("/");
buttonEqual = new JButton("=");
buttonClear = new JButton("C");
panel = new JPanel();
panel.setLayout(new GridLayout(4, 4, 5, 5));
panel.add(button7);
panel.add(button8);
panel.add(button9);
panel.add(buttonDiv);
panel.add(button4);
panel.add(button5);
panel.add(button6);
panel.add(buttonMul);
panel.add(button1);
panel.add(button2);
panel.add(button3);
panel.add(buttonSub);
panel.add(button0);
panel.add(buttonClear);
panel.add(buttonEqual);
panel.add(buttonAdd);
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(textField, BorderLayout.NORTH);
contentPane.add(panel, BorderLayout.CENTER);
setSize(300, 300);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
Calculator calculator = new Calculator();
calculator.setVisible(true);
}
}
```
这个程序使用了JFrame、JPanel、JTextField、JButton等Swing组件,通过setLayout()方法设置了布局方式,实现了Windows10的计算器界面。
阅读全文