窗体完成Java的加减乘除功能
时间: 2023-07-19 15:27:05 浏览: 71
好的,我可以为您提供一个简单的Java窗体实现加减乘除功能的示例代码。您可以在窗体中输入数字和操作符,然后点击“=”按钮得到计算结果。以下是示例代码:
```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 button0, button1, button2, button3, button4,
button5, button6, button7, button8, button9, buttonAdd,
buttonSub, buttonMul, buttonDiv, buttonDot, buttonEqual,
buttonClear, buttonBackspace;
private double num1 = 0.0, num2 = 0.0;
private String operator = "";
public Calculator() {
setTitle("Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setSize(250, 300);
setLocationRelativeTo(null);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(5, 4));
textField = new JTextField(20);
textField.setEditable(false);
panel.add(textField);
buttonClear = new JButton("C");
buttonClear.addActionListener(this);
panel.add(buttonClear);
buttonBackspace = new JButton("←");
buttonBackspace.addActionListener(this);
panel.add(buttonBackspace);
buttonDiv = new JButton("/");
buttonDiv.addActionListener(this);
panel.add(buttonDiv);
button7 = new JButton("7");
button7.addActionListener(this);
panel.add(button7);
button8 = new JButton("8");
button8.addActionListener(this);
panel.add(button8);
button9 = new JButton("9");
button9.addActionListener(this);
panel.add(button9);
buttonMul = new JButton("*");
buttonMul.addActionListener(this);
panel.add(buttonMul);
button4 = new JButton("4");
button4.addActionListener(this);
panel.add(button4);
button5 = new JButton("5");
button5.addActionListener(this);
panel.add(button5);
button6 = new JButton("6");
button6.addActionListener(this);
panel.add(button6);
buttonSub = new JButton("-");
buttonSub.addActionListener(this);
panel.add(buttonSub);
button1 = new JButton("1");
button1.addActionListener(this);
panel.add(button1);
button2 = new JButton("2");
button2.addActionListener(this);
panel.add(button2);
button3 = new JButton("3");
button3.addActionListener(this);
panel.add(button3);
buttonAdd = new JButton("+");
buttonAdd.addActionListener(this);
panel.add(buttonAdd);
button0 = new JButton("0");
button0.addActionListener(this);
panel.add(button0);
buttonDot = new JButton(".");
buttonDot.addActionListener(this);
panel.add(buttonDot);
buttonEqual = new JButton("=");
buttonEqual.addActionListener(this);
panel.add(buttonEqual);
add(panel);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == buttonClear) {
num1 = 0.0;
num2 = 0.0;
operator = "";
textField.setText("");
} else if (e.getSource() == buttonBackspace) {
String text = textField.getText();
if (!text.isEmpty()) {
text = text.substring(0, text.length() - 1);
textField.setText(text);
}
} else if (e.getSource() == buttonAdd || e.getSource() == buttonSub
|| e.getSource() == buttonMul || e.getSource() == buttonDiv) {
num1 = Double.parseDouble(textField.getText());
operator = e.getActionCommand();
textField.setText("");
} else if (e.getSource() == buttonEqual) {
num2 = Double.parseDouble(textField.getText());
double result = 0.0;
switch (operator) {
case "+":
result = num1 + num2;
break;
case "-":
result = num1 - num2;
break;
case "*":
result = num1 * num2;
break;
case "/":
if (num2 == 0) {
JOptionPane.showMessageDialog(this, "Cannot divide by zero!", "Error", JOptionPane.ERROR_MESSAGE);
return;
} else {
result = num1 / num2;
break;
}
}
textField.setText(String.valueOf(result));
} else {
textField.setText(textField.getText() + e.getActionCommand());
}
}
public static void main(String[] args) {
new Calculator();
}
}
```
这个程序实现了基本的加、减、乘、除运算,以及清空输入框、删除最后一个字符等功能。您可以根据需要自行扩展。
阅读全文