利用JAVA完成计算器的加、减、乘、除、开方、求反等功能,具体功能需求可以参考window自带计算器。完成对计算器界面开发的代码,完成计算器逻辑功能的开发
时间: 2023-12-12 07:03:00 浏览: 223
以下是一个简单的利用JAVA实现计算器的例子,包括基本的加、减、乘、除、开方和求反功能:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JTextField textField;
private JButton btn0, btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9;
private JButton btnAdd, btnSub, btnMul, btnDiv, btnDot, btnEql, btnSqrt, btnNeg;
private double num1, num2, result;
private String operator;
public Calculator() {
setTitle("Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(240, 300);
setLocationRelativeTo(null);
textField = new JTextField(15);
textField.setHorizontalAlignment(JTextField.RIGHT);
textField.setEditable(false);
btn0 = new JButton("0");
btn1 = new JButton("1");
btn2 = new JButton("2");
btn3 = new JButton("3");
btn4 = new JButton("4");
btn5 = new JButton("5");
btn6 = new JButton("6");
btn7 = new JButton("7");
btn8 = new JButton("8");
btn9 = new JButton("9");
btnAdd = new JButton("+");
btnSub = new JButton("-");
btnMul = new JButton("*");
btnDiv = new JButton("/");
btnDot = new JButton(".");
btnEql = new JButton("=");
btnSqrt = new JButton("sqrt");
btnNeg = new JButton("+/-");
JPanel panel1 = new JPanel();
panel1.setLayout(new GridLayout(4, 3));
panel1.add(btn1);
panel1.add(btn2);
panel1.add(btn3);
panel1.add(btn4);
panel1.add(btn5);
panel1.add(btn6);
panel1.add(btn7);
panel1.add(btn8);
panel1.add(btn9);
panel1.add(btn0);
panel1.add(btnDot);
panel1.add(btnNeg);
JPanel panel2 = new JPanel();
panel2.setLayout(new GridLayout(4, 1));
panel2.add(btnAdd);
panel2.add(btnSub);
panel2.add(btnMul);
panel2.add(btnDiv);
JPanel panel3 = new JPanel();
panel3.setLayout(new GridLayout(1, 2));
panel3.add(btnSqrt);
panel3.add(btnEql);
JPanel contentPane = new JPanel();
contentPane.setLayout(new BorderLayout());
contentPane.add(textField, BorderLayout.NORTH);
contentPane.add(panel1, BorderLayout.CENTER);
contentPane.add(panel2, BorderLayout.EAST);
contentPane.add(panel3, BorderLayout.SOUTH);
setContentPane(contentPane);
setVisible(true);
btn0.addActionListener(this);
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
btn5.addActionListener(this);
btn6.addActionListener(this);
btn7.addActionListener(this);
btn8.addActionListener(this);
btn9.addActionListener(this);
btnAdd.addActionListener(this);
btnSub.addActionListener(this);
btnMul.addActionListener(this);
btnDiv.addActionListener(this);
btnDot.addActionListener(this);
btnEql.addActionListener(this);
btnSqrt.addActionListener(this);
btnNeg.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
String input = e.getActionCommand();
if (input.equals("C")) {
textField.setText("");
} else if (input.equals("+/-")) {
num1 = Double.parseDouble(textField.getText());
num1 = -num1;
textField.setText(Double.toString(num1));
} else if (input.equals("sqrt")) {
num1 = Double.parseDouble(textField.getText());
num1 = Math.sqrt(num1);
textField.setText(Double.toString(num1));
} else if (input.equals("+")) {
num1 = Double.parseDouble(textField.getText());
operator = "+";
textField.setText("");
} else if (input.equals("-")) {
num1 = Double.parseDouble(textField.getText());
operator = "-";
textField.setText("");
} else if (input.equals("*")) {
num1 = Double.parseDouble(textField.getText());
operator = "*";
textField.setText("");
} else if (input.equals("/")) {
num1 = Double.parseDouble(textField.getText());
operator = "/";
textField.setText("");
} else if (input.equals("=")) {
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 {
String currentText = textField.getText();
currentText += input;
textField.setText(currentText);
}
}
public static void main(String[] args) {
new Calculator();
}
}
```
该程序使用了Java Swing库来创建GUI,实现了基本的加、减、乘、除、开方和求反功能。用户可以通过按钮输入数字和操作符,并且计算器会在文本框中显示结果。
阅读全文