用Java设计一个简单的计算器,要求要有相应的图形界面,有相加,相减,相除,相乘与清 除功能,并且为按钮设计相应的事件
时间: 2024-05-07 09:22:54 浏览: 135
基于java的计算器..AWT的图形界面,具有加法,减法,乘法,除法等计算功能.zip
以下是基于Java Swing的简单计算器实现,包含加、减、乘、除和清除功能:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class SimpleCalculator extends JFrame {
private JTextField inputField;
private JLabel errorLabel;
private double currentResult;
private String currentOperator;
public SimpleCalculator() {
super("Simple Calculator");
//初始化当前结果和操作符
currentResult = 0;
currentOperator = "";
//创建输入框和错误提示标签
inputField = new JTextField(10);
inputField.setEditable(false);
inputField.setHorizontalAlignment(JTextField.RIGHT);
errorLabel = new JLabel("");
errorLabel.setForeground(Color.RED);
errorLabel.setHorizontalAlignment(JLabel.RIGHT);
//创建数字按钮
JButton[] digitButtons = new JButton[10];
for (int i = 0; i < 10; i++) {
digitButtons[i] = new JButton(Integer.toString(i));
digitButtons[i].addActionListener(new DigitButtonActionListener(i));
}
//创建操作符按钮
JButton addButton = new JButton("+");
addButton.addActionListener(new OperatorButtonActionListener("+"));
JButton subtractButton = new JButton("-");
subtractButton.addActionListener(new OperatorButtonActionListener("-"));
JButton multiplyButton = new JButton("*");
multiplyButton.addActionListener(new OperatorButtonActionListener("*"));
JButton divideButton = new JButton("/");
divideButton.addActionListener(new OperatorButtonActionListener("/"));
//创建清除按钮
JButton clearButton = new JButton("C");
clearButton.addActionListener(new ClearButtonActionListener());
//创建面板并添加组件
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 4;
gbc.fill = GridBagConstraints.HORIZONTAL;
panel.add(inputField, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 4;
gbc.fill = GridBagConstraints.HORIZONTAL;
panel.add(errorLabel, gbc);
gbc.gridx = 0;
gbc.gridy = 2;
gbc.gridwidth = 1;
panel.add(digitButtons[7], gbc);
gbc.gridx = 1;
gbc.gridy = 2;
panel.add(digitButtons[8], gbc);
gbc.gridx = 2;
gbc.gridy = 2;
panel.add(digitButtons[9], gbc);
gbc.gridx = 3;
gbc.gridy = 2;
panel.add(addButton, gbc);
gbc.gridx = 0;
gbc.gridy = 3;
panel.add(digitButtons[4], gbc);
gbc.gridx = 1;
gbc.gridy = 3;
panel.add(digitButtons[5], gbc);
gbc.gridx = 2;
gbc.gridy = 3;
panel.add(digitButtons[6], gbc);
gbc.gridx = 3;
gbc.gridy = 3;
panel.add(subtractButton, gbc);
gbc.gridx = 0;
gbc.gridy = 4;
panel.add(digitButtons[1], gbc);
gbc.gridx = 1;
gbc.gridy = 4;
panel.add(digitButtons[2], gbc);
gbc.gridx = 2;
gbc.gridy = 4;
panel.add(digitButtons[3], gbc);
gbc.gridx = 3;
gbc.gridy = 4;
panel.add(multiplyButton, gbc);
gbc.gridx = 0;
gbc.gridy = 5;
panel.add(digitButtons[0], gbc);
gbc.gridx = 1;
gbc.gridy = 5;
panel.add(clearButton, gbc);
gbc.gridx = 2;
gbc.gridy = 5;
panel.add(new JLabel(), gbc);
gbc.gridx = 3;
gbc.gridy = 5;
panel.add(divideButton, gbc);
//添加面板到窗口
add(panel);
//设置窗口属性
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 200);
setLocationRelativeTo(null); //居中显示
setResizable(false);
setVisible(true);
}
private class DigitButtonActionListener implements ActionListener {
private int digit;
public DigitButtonActionListener(int digit) {
this.digit = digit;
}
@Override
public void actionPerformed(ActionEvent e) {
//将数字添加到输入框
inputField.setText(inputField.getText() + Integer.toString(digit));
errorLabel.setText("");
}
}
private class OperatorButtonActionListener implements ActionListener {
private String operator;
public OperatorButtonActionListener(String operator) {
this.operator = operator;
}
@Override
public void actionPerformed(ActionEvent e) {
try {
//将当前输入的数字转换为double类型
double number = Double.parseDouble(inputField.getText());
//根据当前操作符进行计算
switch (currentOperator) {
case "+":
currentResult += number;
break;
case "-":
currentResult -= number;
break;
case "*":
currentResult *= number;
break;
case "/":
currentResult /= number;
break;
default:
currentResult = number;
break;
}
//更新当前操作符和输入框的值
currentOperator = operator;
inputField.setText(Double.toString(currentResult));
errorLabel.setText("");
} catch (NumberFormatException ex) {
//输入的不是数字,显示错误提示
errorLabel.setText("Invalid input");
}
}
}
private class ClearButtonActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
//清空输入框和当前结果,重置操作符
inputField.setText("");
currentResult = 0;
currentOperator = "";
errorLabel.setText("");
}
}
public static void main(String[] args) {
new SimpleCalculator();
}
}
```
运行该程序,将会出现一个简单的计算器界面。用户可以通过点击数字按钮输入数字,通过点击加、减、乘、除按钮进行计算,通过点击清除按钮清空输入框和当前结果。
注:此代码为示例代码,仅供参考,实际应用中需要根据需求进行修改和完善。
阅读全文