编写程序实现一个简单的运算器,要求至少实现加、减、乘、除四则运算,运算器功能界面排布尽量接近实际的运算器,数字输入通过界面上的数字按钮进行。java
时间: 2024-05-13 21:18:50 浏览: 57
java 简单计算器(实现加、减、乘、除功能)
5星 · 资源好评率100%
以下是一个基本的Java程序,实现了加、减、乘、除四则运算:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener {
// 定义界面组件
private JTextField display;
private JButton button0, button1, button2, button3, button4, button5, button6, button7, button8, button9;
private JButton buttonPlus, buttonMinus, buttonMultiply, buttonDivide, buttonEqual, buttonClear;
// 定义变量
private double prevValue, curValue;
private String operator;
public Calculator() {
// 初始化变量
prevValue = 0.0;
curValue = 0.0;
operator = "";
// 创建界面组件
display = new JTextField();
display.setEditable(false);
display.setHorizontalAlignment(JTextField.RIGHT);
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");
buttonPlus = new JButton("+");
buttonMinus = new JButton("-");
buttonMultiply = new JButton("*");
buttonDivide = new JButton("/");
buttonEqual = new JButton("=");
buttonClear = new JButton("C");
// 添加事件监听器
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);
buttonPlus.addActionListener(this);
buttonMinus.addActionListener(this);
buttonMultiply.addActionListener(this);
buttonDivide.addActionListener(this);
buttonEqual.addActionListener(this);
buttonClear.addActionListener(this);
// 创建界面布局
JPanel panel1 = new JPanel(new GridLayout(1, 1));
panel1.add(display);
JPanel panel2 = new JPanel(new GridLayout(4, 4));
panel2.add(button7);
panel2.add(button8);
panel2.add(button9);
panel2.add(buttonDivide);
panel2.add(button4);
panel2.add(button5);
panel2.add(button6);
panel2.add(buttonMultiply);
panel2.add(button1);
panel2.add(button2);
panel2.add(button3);
panel2.add(buttonMinus);
panel2.add(button0);
panel2.add(buttonClear);
panel2.add(buttonEqual);
panel2.add(buttonPlus);
// 添加组件到窗口
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(panel1, BorderLayout.NORTH);
contentPane.add(panel2, BorderLayout.CENTER);
// 设置窗口属性
setTitle("Calculator");
setSize(300, 300);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
}
// 处理按钮点击事件
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.equals("C")) {
display.setText("");
prevValue = 0.0;
curValue = 0.0;
operator = "";
} else if (command.equals("+")) {
prevValue = Double.parseDouble(display.getText());
display.setText("");
operator = "+";
} else if (command.equals("-")) {
prevValue = Double.parseDouble(display.getText());
display.setText("");
operator = "-";
} else if (command.equals("*")) {
prevValue = Double.parseDouble(display.getText());
display.setText("");
operator = "*";
} else if (command.equals("/")) {
prevValue = Double.parseDouble(display.getText());
display.setText("");
operator = "/";
} else if (command.equals("=")) {
curValue = Double.parseDouble(display.getText());
if (operator.equals("+")) {
display.setText(Double.toString(prevValue + curValue));
} else if (operator.equals("-")) {
display.setText(Double.toString(prevValue - curValue));
} else if (operator.equals("*")) {
display.setText(Double.toString(prevValue * curValue));
} else if (operator.equals("/")) {
if (curValue == 0.0) {
display.setText("Error");
} else {
display.setText(Double.toString(prevValue / curValue));
}
}
prevValue = 0.0;
curValue = 0.0;
operator = "";
} else {
display.setText(display.getText() + command);
}
}
public static void main(String[] args) {
Calculator calc = new Calculator();
calc.setVisible(true);
}
}
```
这个程序创建了一个窗口,其中包含了一个文本框和一些按钮。每个按钮都有一个事件监听器,当用户点击按钮时,程序会根据按钮的标签执行相应的操作。
阅读全文