使用java语言编写一个具有图形化界面的科学计算器
时间: 2024-03-10 09:50:39 浏览: 74
好的,我可以为您提供一个使用Java语言编写的具有图形化界面的科学计算器示例。这里使用的是Java Swing库实现界面设计。代码如下:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ScientificCalculator extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JTextField textField;
private double num1, num2, result;
private int operation;
public ScientificCalculator() {
setTitle("科学计算器");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setSize(300, 400);
setResizable(false);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(5, 4, 5, 5));
textField = new JTextField();
textField.setEditable(false);
textField.setHorizontalAlignment(SwingConstants.RIGHT);
panel.add(textField);
JButton button1 = new JButton("1");
button1.addActionListener(this);
panel.add(button1);
JButton button2 = new JButton("2");
button2.addActionListener(this);
panel.add(button2);
JButton button3 = new JButton("3");
button3.addActionListener(this);
panel.add(button3);
JButton buttonAdd = new JButton("+");
buttonAdd.addActionListener(this);
panel.add(buttonAdd);
JButton button4 = new JButton("4");
button4.addActionListener(this);
panel.add(button4);
JButton button5 = new JButton("5");
button5.addActionListener(this);
panel.add(button5);
JButton button6 = new JButton("6");
button6.addActionListener(this);
panel.add(button6);
JButton buttonSub = new JButton("-");
buttonSub.addActionListener(this);
panel.add(buttonSub);
JButton button7 = new JButton("7");
button7.addActionListener(this);
panel.add(button7);
JButton button8 = new JButton("8");
button8.addActionListener(this);
panel.add(button8);
JButton button9 = new JButton("9");
button9.addActionListener(this);
panel.add(button9);
JButton buttonMult = new JButton("*");
buttonMult.addActionListener(this);
panel.add(buttonMult);
JButton button0 = new JButton("0");
button0.addActionListener(this);
panel.add(button0);
JButton buttonDot = new JButton(".");
buttonDot.addActionListener(this);
panel.add(buttonDot);
JButton buttonEquals = new JButton("=");
buttonEquals.addActionListener(this);
panel.add(buttonEquals);
JButton buttonDiv = new JButton("/");
buttonDiv.addActionListener(this);
panel.add(buttonDiv);
JButton buttonPow = new JButton("^");
buttonPow.addActionListener(this);
panel.add(buttonPow);
JButton buttonSqrt = new JButton("√");
buttonSqrt.addActionListener(this);
panel.add(buttonSqrt);
JButton buttonClear = new JButton("C");
buttonClear.addActionListener(this);
panel.add(buttonClear);
add(panel, BorderLayout.CENTER);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String buttonText = e.getActionCommand();
if (buttonText.equals("C")) {
textField.setText("");
num1 = 0;
num2 = 0;
result = 0;
operation = 0;
} else if (buttonText.equals("+")) {
num1 = Double.parseDouble(textField.getText());
operation = 1;
textField.setText("");
} else if (buttonText.equals("-")) {
num1 = Double.parseDouble(textField.getText());
operation = 2;
textField.setText("");
} else if (buttonText.equals("*")) {
num1 = Double.parseDouble(textField.getText());
operation = 3;
textField.setText("");
} else if (buttonText.equals("/")) {
num1 = Double.parseDouble(textField.getText());
operation = 4;
textField.setText("");
} else if (buttonText.equals("^")) {
num1 = Double.parseDouble(textField.getText());
operation = 5;
textField.setText("");
} else if (buttonText.equals("√")) {
num1 = Double.parseDouble(textField.getText());
result = Math.sqrt(num1);
textField.setText(String.valueOf(result));
} else if (buttonText.equals("=")) {
num2 = Double.parseDouble(textField.getText());
switch (operation) {
case 1:
result = num1 + num2;
break;
case 2:
result = num1 - num2;
break;
case 3:
result = num1 * num2;
break;
case 4:
result = num1 / num2;
break;
case 5:
result = Math.pow(num1, num2);
break;
}
textField.setText(String.valueOf(result));
} else {
textField.setText(textField.getText() + buttonText);
}
}
public static void main(String[] args) {
new ScientificCalculator();
}
}
```
这个示例使用了JFrame、JPanel、JTextField和JButton等Swing组件,实现了基本的数学运算、幂运算和开方等功能。你可以根据自己的需要添加更多的功能和组件。
阅读全文