javagui中做一个图形界面计算器包含四则运算,带Π的三角函数运算,并可以清空界面
时间: 2023-11-22 22:55:00 浏览: 87
下面是一个简单的Java GUI计算器的实现,可以进行四则运算和三角函数运算,并且可以清空界面:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
public class CalculatorGUI extends JFrame implements ActionListener {
private final JButton[] numberButtons = new JButton[10];
private final JButton[] functionButtons = new JButton[8];
private final JButton addButton, minusButton, multiplyButton, divideButton, dotButton, equalButton, clearButton, piButton;
private final JTextField textField;
private double num1 = 0, num2 = 0;
private char operator;
private boolean isDotPressed = false;
public CalculatorGUI() {
setTitle("Java GUI Calculator");
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 4;
textField = new JTextField();
textField.setEditable(false);
panel.add(textField, gbc);
gbc.gridwidth = 1;
gbc.gridy = 1;
clearButton = new JButton("C");
clearButton.addActionListener(this);
panel.add(clearButton, gbc);
piButton = new JButton("π");
piButton.addActionListener(this);
panel.add(piButton, gbc);
addButton = new JButton("+");
addButton.addActionListener(this);
panel.add(addButton, gbc);
minusButton = new JButton("-");
minusButton.addActionListener(this);
panel.add(minusButton, gbc);
multiplyButton = new JButton("*");
multiplyButton.addActionListener(this);
panel.add(multiplyButton, gbc);
divideButton = new JButton("/");
divideButton.addActionListener(this);
panel.add(divideButton, gbc);
gbc.gridx = 0;
gbc.gridy = 2;
for (int i = 0; i < 10; i++) {
numberButtons[i] = new JButton(String.valueOf(i));
numberButtons[i].addActionListener(this);
if (i == 0) {
gbc.gridwidth = 2;
panel.add(numberButtons[i], gbc);
gbc.gridwidth = 1;
} else {
panel.add(numberButtons[i], gbc);
}
gbc.gridx++;
if (gbc.gridx > 2) {
gbc.gridx = 0;
gbc.gridy++;
}
}
dotButton = new JButton(".");
dotButton.addActionListener(this);
panel.add(dotButton, gbc);
equalButton = new JButton("=");
equalButton.addActionListener(this);
panel.add(equalButton, gbc);
functionButtons[0] = new JButton("sin");
functionButtons[1] = new JButton("cos");
functionButtons[2] = new JButton("tan");
functionButtons[3] = new JButton("asin");
functionButtons[4] = new JButton("acos");
functionButtons[5] = new JButton("atan");
functionButtons[6] = new JButton("sqrt");
functionButtons[7] = new JButton("log");
gbc.gridy++;
for (int i = 0; i < 8; i++) {
functionButtons[i].addActionListener(this);
panel.add(functionButtons[i], gbc);
gbc.gridx++;
if (gbc.gridx > 2) {
gbc.gridx = 0;
gbc.gridy++;
}
}
add(panel);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == clearButton) {
textField.setText("");
num1 = 0;
num2 = 0;
operator = ' ';
isDotPressed = false;
} else if (e.getSource() == piButton) {
textField.setText(String.valueOf(Math.PI));
num1 = Math.PI;
} else if (e.getSource() == addButton) {
num1 = Double.parseDouble(textField.getText());
operator = '+';
textField.setText("");
isDotPressed = false;
} else if (e.getSource() == minusButton) {
num1 = Double.parseDouble(textField.getText());
operator = '-';
textField.setText("");
isDotPressed = false;
} else if (e.getSource() == multiplyButton) {
num1 = Double.parseDouble(textField.getText());
operator = '*';
textField.setText("");
isDotPressed = false;
} else if (e.getSource() == divideButton) {
num1 = Double.parseDouble(textField.getText());
operator = '/';
textField.setText("");
isDotPressed = false;
} else if (e.getSource() == equalButton) {
num2 = Double.parseDouble(textField.getText());
switch (operator) {
case '+':
textField.setText(String.valueOf(num1 + num2));
break;
case '-':
textField.setText(String.valueOf(num1 - num2));
break;
case '*':
textField.setText(String.valueOf(num1 * num2));
break;
case '/':
textField.setText(String.valueOf(num1 / num2));
break;
}
num1 = Double.parseDouble(textField.getText());
num2 = 0;
isDotPressed = false;
} else if (e.getSource() == dotButton) {
if (!isDotPressed) {
textField.setText(textField.getText() + ".");
isDotPressed = true;
}
} else if (e.getSource() == functionButtons[0]) {
double value = Double.parseDouble(textField.getText());
double result = Math.sin(Math.toRadians(value));
textField.setText(String.valueOf(result));
num1 = result;
isDotPressed = false;
} else if (e.getSource() == functionButtons[1]) {
double value = Double.parseDouble(textField.getText());
double result = Math.cos(Math.toRadians(value));
textField.setText(String.valueOf(result));
num1 = result;
isDotPressed = false;
} else if (e.getSource() == functionButtons[2]) {
double value = Double.parseDouble(textField.getText());
double result = Math.tan(Math.toRadians(value));
textField.setText(String.valueOf(result));
num1 = result;
isDotPressed = false;
} else if (e.getSource() == functionButtons[3]) {
double value = Double.parseDouble(textField.getText());
double result = Math.asin(Math.toRadians(value));
textField.setText(String.valueOf(result));
num1 = result;
isDotPressed = false;
} else if (e.getSource() == functionButtons[4]) {
double value = Double.parseDouble(textField.getText());
double result = Math.acos(Math.toRadians(value));
textField.setText(String.valueOf(result));
num1 = result;
isDotPressed = false;
} else if (e.getSource() == functionButtons[5]) {
double value = Double.parseDouble(textField.getText());
double result = Math.atan(Math.toRadians(value));
textField.setText(String.valueOf(result));
num1 = result;
isDotPressed = false;
} else if (e.getSource() == functionButtons[6]) {
double value = Double.parseDouble(textField.getText());
double result = Math.sqrt(value);
textField.setText(String.valueOf(result));
num1 = result;
isDotPressed = false;
} else if (e.getSource() == functionButtons[7]) {
double value = Double.parseDouble(textField.getText());
double result = Math.log10(value);
textField.setText(String.valueOf(result));
num1 = result;
isDotPressed = false;
} else {
for (int i = 0; i < 10; i++) {
if (e.getSource() == numberButtons[i]) {
textField.setText(textField.getText() + i);
num1 = Double.parseDouble(textField.getText());
break;
}
}
}
}
public static void main(String[] args) {
new CalculatorGUI();
}
}
```
该计算器支持输入数字、小数点、四则运算符号、Π、清空操作以及一些基本的三角函数和其他函数。界面使用了Java Swing库中的JPanel和JButton组件,通过GridBagLayout布局实现了简单的排版。在每次操作后,将当前的操作数和运算符保存下来,以便进行连续计算,并在等号按钮被按下时根据运算符进行计算。对于一些特殊的函数操作(例如sin、cos、tan等),使用了Java Math库中的函数实现。
阅读全文