用Java语言写一个计算器界面,实现如下科学计算器界面,24个按键,并为按钮添加事件处理:当按钮被点击时将对应字符显示到顶部文本框中,使用Java Swing编写的简单计算器界面代码,还需要科学计算器
时间: 2024-05-10 22:15:28 浏览: 65
Java 语言编写科学计算器界面的设计
以下是一个简单的 Java Swing 科学计算器界面代码,实现了 24 个按键和对应的事件处理:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener {
private JTextField textField;
private JButton button0, button1, button2, button3, button4, button5, button6, button7, button8, button9;
private JButton buttonAdd, buttonSubtract, buttonMultiply, buttonDivide, buttonEquals, buttonClear;
private JButton buttonSqrt, buttonPow, buttonSin, buttonCos, buttonTan, buttonLog, buttonLn, buttonPi;
private double num1, num2, result;
private String operator;
public Calculator() {
super("Scientific Calculator");
textField = new JTextField(30);
textField.setEditable(false);
textField.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");
buttonAdd = new JButton("+");
buttonSubtract = new JButton("-");
buttonMultiply = new JButton("*");
buttonDivide = new JButton("/");
buttonEquals = new JButton("=");
buttonClear = new JButton("C");
buttonSqrt = new JButton("sqrt");
buttonPow = new JButton("pow");
buttonSin = new JButton("sin");
buttonCos = new JButton("cos");
buttonTan = new JButton("tan");
buttonLog = new JButton("log");
buttonLn = new JButton("ln");
buttonPi = new JButton("pi");
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(6, 4, 5, 5));
buttonPanel.add(buttonSqrt);
buttonPanel.add(buttonPow);
buttonPanel.add(buttonSin);
buttonPanel.add(buttonCos);
buttonPanel.add(button7);
buttonPanel.add(button8);
buttonPanel.add(button9);
buttonPanel.add(buttonDivide);
buttonPanel.add(button4);
buttonPanel.add(button5);
buttonPanel.add(button6);
buttonPanel.add(buttonMultiply);
buttonPanel.add(button1);
buttonPanel.add(button2);
buttonPanel.add(button3);
buttonPanel.add(buttonSubtract);
buttonPanel.add(button0);
buttonPanel.add(buttonPi);
buttonPanel.add(buttonClear);
buttonPanel.add(buttonAdd);
buttonPanel.add(buttonLog);
buttonPanel.add(buttonLn);
buttonPanel.add(buttonTan);
buttonPanel.add(buttonEquals);
Container container = getContentPane();
container.setLayout(new BorderLayout(5, 5));
container.add(textField, BorderLayout.NORTH);
container.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);
buttonSqrt.addActionListener(this);
buttonPow.addActionListener(this);
buttonSin.addActionListener(this);
buttonCos.addActionListener(this);
buttonTan.addActionListener(this);
buttonLog.addActionListener(this);
buttonLn.addActionListener(this);
buttonPi.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button0) {
textField.setText(textField.getText() + "0");
} else if (e.getSource() == button1) {
textField.setText(textField.getText() + "1");
} else if (e.getSource() == button2) {
textField.setText(textField.getText() + "2");
} else if (e.getSource() == button3) {
textField.setText(textField.getText() + "3");
} else if (e.getSource() == button4) {
textField.setText(textField.getText() + "4");
} else if (e.getSource() == button5) {
textField.setText(textField.getText() + "5");
} else if (e.getSource() == button6) {
textField.setText(textField.getText() + "6");
} else if (e.getSource() == button7) {
textField.setText(textField.getText() + "7");
} else if (e.getSource() == button8) {
textField.setText(textField.getText() + "8");
} else if (e.getSource() == button9) {
textField.setText(textField.getText() + "9");
} else if (e.getSource() == buttonAdd) {
num1 = Double.parseDouble(textField.getText());
operator = "+";
textField.setText("");
} else if (e.getSource() == buttonSubtract) {
num1 = Double.parseDouble(textField.getText());
operator = "-";
textField.setText("");
} else if (e.getSource() == buttonMultiply) {
num1 = Double.parseDouble(textField.getText());
operator = "*";
textField.setText("");
} else if (e.getSource() == buttonDivide) {
num1 = Double.parseDouble(textField.getText());
operator = "/";
textField.setText("");
} else if (e.getSource() == buttonEquals) {
num2 = Double.parseDouble(textField.getText());
if (operator.equals("+")) {
result = num1 + num2;
} else if (operator.equals("-")) {
result = num1 - num2;
} else if (operator.equals("*")) {
result = num1 * num2;
} else if (operator.equals("/")) {
result = num1 / num2;
}
textField.setText(Double.toString(result));
} else if (e.getSource() == buttonClear) {
textField.setText("");
} else if (e.getSource() == buttonSqrt) {
num1 = Double.parseDouble(textField.getText());
result = Math.sqrt(num1);
textField.setText(Double.toString(result));
} else if (e.getSource() == buttonPow) {
num1 = Double.parseDouble(textField.getText());
operator = "pow";
textField.setText("");
} else if (e.getSource() == buttonSin) {
num1 = Double.parseDouble(textField.getText());
result = Math.sin(num1);
textField.setText(Double.toString(result));
} else if (e.getSource() == buttonCos) {
num1 = Double.parseDouble(textField.getText());
result = Math.cos(num1);
textField.setText(Double.toString(result));
} else if (e.getSource() == buttonTan) {
num1 = Double.parseDouble(textField.getText());
result = Math.tan(num1);
textField.setText(Double.toString(result));
} else if (e.getSource() == buttonLog) {
num1 = Double.parseDouble(textField.getText());
result = Math.log10(num1);
textField.setText(Double.toString(result));
} else if (e.getSource() == buttonLn) {
num1 = Double.parseDouble(textField.getText());
result = Math.log(num1);
textField.setText(Double.toString(result));
} else if (e.getSource() == buttonPi) {
textField.setText(Double.toString(Math.PI));
}
}
public static void main(String[] args) {
Calculator calculator = new Calculator();
calculator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
calculator.setSize(400, 300);
calculator.setLocationRelativeTo(null);
calculator.setVisible(true);
}
}
```
注意:以上代码只是一个简单的实现,还有很多缺陷和需要改进的地方,如输入错误处理、小数点处理、括号支持等等。
阅读全文