用java设计一个计算器窗口
时间: 2023-11-16 20:07:54 浏览: 157
好的,以下是一个简单的Java计算器窗口设计示例:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CalculatorGUI extends JFrame implements ActionListener {
private JTextField display;
private JButton button0, button1, button2, button3, button4, button5, button6, button7, button8, button9;
private JButton buttonAdd, buttonSubtract, buttonMultiply, buttonDivide, buttonEquals, buttonClear;
private double num1 = 0.0, num2 = 0.0;
private char operator = ' ';
public CalculatorGUI() {
super("Calculator");
// 创建显示框
display = new JTextField(20);
display.setHorizontalAlignment(JTextField.RIGHT);
display.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("+");
buttonSubtract = new JButton("-");
buttonMultiply = new JButton("*");
buttonDivide = new JButton("/");
buttonEquals = new JButton("=");
buttonClear = new JButton("C");
// 设置布局和添加组件
setLayout(new BorderLayout());
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4, 4));
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);
add(display, BorderLayout.NORTH);
add(buttonPanel, BorderLayout.CENTER);
// 为按钮添加监听器
button0.addActionListener(this);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
button6.addActionListener(this);
button7.addActionListener(this);
button8.addActionListener(this);
button9.addActionListener(this);
buttonAdd.addActionListener(this);
buttonSubtract.addActionListener(this);
buttonMultiply.addActionListener(this);
buttonDivide.addActionListener(this);
buttonEquals.addActionListener(this);
buttonClear.addActionListener(this);
}
// 实现ActionListener接口中的方法
public void actionPerformed(ActionEvent event) {
if (event.getSource() == button0) {
display.setText(display.getText() + "0");
} else if (event.getSource() == button1) {
display.setText(display.getText() + "1");
} else if (event.getSource() == button2) {
display.setText(display.getText() + "2");
} else if (event.getSource() == button3) {
display.setText(display.getText() + "3");
} else if (event.getSource() == button4) {
display.setText(display.getText() + "4");
} else if (event.getSource() == button5) {
display.setText(display.getText() + "5");
} else if (event.getSource() == button6) {
display.setText(display.getText() + "6");
} else if (event.getSource() == button7) {
display.setText(display.getText() + "7");
} else if (event.getSource() == button8) {
display.setText(display.getText() + "8");
} else if (event.getSource() == button9) {
display.setText(display.getText() + "9");
} else if (event.getSource() == buttonAdd) {
num1 = Double.parseDouble(display.getText());
operator = '+';
display.setText("");
} else if (event.getSource() == buttonSubtract) {
num1 = Double.parseDouble(display.getText());
operator = '-';
display.setText("");
} else if (event.getSource() == buttonMultiply) {
num1 = Double.parseDouble(display.getText());
operator = '*';
display.setText("");
} else if (event.getSource() == buttonDivide) {
num1 = Double.parseDouble(display.getText());
operator = '/';
display.setText("");
} else if (event.getSource() == buttonEquals) {
num2 = Double.parseDouble(display.getText());
switch (operator) {
case '+':
display.setText(Double.toString(num1 + num2));
break;
case '-':
display.setText(Double.toString(num1 - num2));
break;
case '*':
display.setText(Double.toString(num1 * num2));
break;
case '/':
display.setText(Double.toString(num1 / num2));
break;
}
} else if (event.getSource() == buttonClear) {
display.setText("");
num1 = 0.0;
num2 = 0.0;
operator = ' ';
}
}
public static void main(String[] args) {
CalculatorGUI calculator = new CalculatorGUI();
calculator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
calculator.setSize(300, 300);
calculator.setVisible(true);
}
}
```
这个计算器窗口可以接受用户的输入,并根据用户的操作进行相应的计算,最后在窗口上显示计算结果。运行程序后,它会显示一个包含数字按钮、运算符按钮和一个文本框的窗口,用户可以通过点击按钮来输入数字和运算符,最后点击“=”按钮来进行计算,计算结果会显示在文本框中。
阅读全文