写一个Java gui计算器实现两个数之间的加减乘除,用户输入一个小数点后在输入数字则程序应该自动在小数点前面补充 0
时间: 2024-02-06 11:10:18 浏览: 76
java编程之计算器的设计
以下是一个简单的Java GUI计算器,支持两个数之间的加减乘除,并且在用户输入小数点后自动补充前导0。
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Calculator extends JFrame implements ActionListener {
private JTextField textField;
private JButton btn0, btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9;
private JButton btnAdd, btnSub, btnMul, btnDiv, btnDot, btnClear, btnEqual;
private double num1 = 0, num2 = 0;
private String operator = "";
public Calculator() {
initGUI();
}
private void initGUI() {
setTitle("Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationByPlatform(true);
// 创建组件
textField = new JTextField(20);
textField.setEditable(false);
textField.setHorizontalAlignment(JTextField.RIGHT);
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(".");
btnClear = new JButton("C");
btnEqual = new JButton("=");
// 添加监听器
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);
btnClear.addActionListener(this);
btnEqual.addActionListener(this);
// 创建面板
JPanel panel = new JPanel(new GridLayout(4, 4));
panel.add(btn7);
panel.add(btn8);
panel.add(btn9);
panel.add(btnAdd);
panel.add(btn4);
panel.add(btn5);
panel.add(btn6);
panel.add(btnSub);
panel.add(btn1);
panel.add(btn2);
panel.add(btn3);
panel.add(btnMul);
panel.add(btn0);
panel.add(btnDot);
panel.add(btnClear);
panel.add(btnDiv);
// 添加组件到窗口
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(textField, BorderLayout.NORTH);
contentPane.add(panel, BorderLayout.CENTER);
// 显示窗口
pack();
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.equals("C")) {
textField.setText("");
num1 = 0;
num2 = 0;
operator = "";
} else if (command.equals("=")) {
if (!operator.equals("") && !textField.getText().equals("")) {
num2 = Double.parseDouble(textField.getText());
double result = calculate(num1, num2, operator);
textField.setText(String.valueOf(result));
num1 = result;
num2 = 0;
operator = "";
}
} else if (command.equals("+") || command.equals("-") || command.equals("*") || command.equals("/")) {
if (!textField.getText().equals("")) {
num1 = Double.parseDouble(textField.getText());
operator = command;
textField.setText("");
}
} else if (command.equals(".")) {
if (textField.getText().equals("")) {
textField.setText("0.");
} else if (!textField.getText().contains(".")) {
textField.setText(textField.getText() + ".");
}
} else {
if (textField.getText().equals("0")) {
textField.setText(command);
} else {
textField.setText(textField.getText() + command);
}
}
}
private double calculate(double num1, double num2, String operator) {
double result = 0;
switch (operator) {
case "+":
result = num1 + num2;
break;
case "-":
result = num1 - num2;
break;
case "*":
result = num1 * num2;
break;
case "/":
result = num1 / num2;
break;
}
return result;
}
public static void main(String[] args) {
new Calculator();
}
}
```
注意,上述代码并没有处理除以0的情况,这是因为Java中,除以0的结果是Infinity或者NaN,这些结果可以直接输出到界面上。如果需要更加完善的计算器逻辑,可以在calculate()方法中添加更多的判断逻辑。
阅读全文