java实现简易计算器 设计和编写一个简易的计算器:实现计算器的界面;实现计算按钮不同的计算功能;显示计算器按钮运算后的结果。
时间: 2023-11-22 08:54:10 浏览: 132
首先,我们需要设计计算器的界面。可以采用Java Swing库来实现。
以下是一个简单的计算器界面设计:
```java
import javax.swing.*;
import java.awt.*;
public class CalculatorUI extends JFrame {
private JTextField displayField;
public CalculatorUI() {
initComponents();
}
private void initComponents() {
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setPreferredSize(new Dimension(300, 300));
displayField = new JTextField();
displayField.setPreferredSize(new Dimension(250, 30));
JPanel buttonPanel = new JPanel(new GridLayout(4, 4, 5, 5));
String[] buttonLabels = {
"7", "8", "9", "/",
"4", "5", "6", "*",
"1", "2", "3", "-",
"0", ".", "=", "+"
};
for (String label : buttonLabels) {
JButton button = new JButton(label);
buttonPanel.add(button);
}
JPanel contentPanel = new JPanel(new BorderLayout());
contentPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
contentPanel.add(displayField, BorderLayout.NORTH);
contentPanel.add(buttonPanel, BorderLayout.CENTER);
getContentPane().add(contentPanel);
pack();
}
public static void main(String[] args) {
new CalculatorUI().setVisible(true);
}
}
```
接下来,我们需要实现计算器按钮的不同计算功能。我们可以使用Java的ScriptEngine类来解析计算表达式并计算结果。
```java
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class Calculator {
private ScriptEngine engine;
public Calculator() {
ScriptEngineManager mgr = new ScriptEngineManager();
engine = mgr.getEngineByName("JavaScript");
}
public double evaluate(String expression) throws ScriptException {
return (double) engine.eval(expression);
}
}
```
最后,我们需要将计算结果显示在计算器界面上。我们可以为每个计算按钮添加ActionListener,当用户点击按钮时,我们将所点击的按钮的文本内容添加到计算器显示文本框中,并在用户点击“=”按钮时计算表达式并显示结果。
```java
import javax.script.ScriptException;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class CalculatorUI extends JFrame {
private JTextField displayField;
private Calculator calculator;
public CalculatorUI() {
initComponents();
calculator = new Calculator();
}
private void initComponents() {
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setPreferredSize(new Dimension(300, 300));
displayField = new JTextField();
displayField.setPreferredSize(new Dimension(250, 30));
JPanel buttonPanel = new JPanel(new GridLayout(4, 4, 5, 5));
String[] buttonLabels = {
"7", "8", "9", "/",
"4", "5", "6", "*",
"1", "2", "3", "-",
"0", ".", "=", "+"
};
for (String label : buttonLabels) {
JButton button = new JButton(label);
button.addActionListener(new CalculatorButtonActionListener());
buttonPanel.add(button);
}
JPanel contentPanel = new JPanel(new BorderLayout());
contentPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
contentPanel.add(displayField, BorderLayout.NORTH);
contentPanel.add(buttonPanel, BorderLayout.CENTER);
getContentPane().add(contentPanel);
pack();
}
private class CalculatorButtonActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String buttonLabel = ((JButton) e.getSource()).getText();
if (buttonLabel.equals("=")) {
try {
String expression = displayField.getText();
double result = calculator.evaluate(expression);
displayField.setText(Double.toString(result));
} catch (ScriptException ex) {
JOptionPane.showMessageDialog(CalculatorUI.this,
"Invalid expression", "Error", JOptionPane.ERROR_MESSAGE);
}
} else {
displayField.setText(displayField.getText() + buttonLabel);
}
}
}
public static void main(String[] args) {
new CalculatorUI().setVisible(true);
}
}
```
阅读全文