Java实现计算器界面,并动态实现加,减,乘,除运算
时间: 2023-07-11 22:10:32 浏览: 39
以下是一个简单的Java计算器界面,实现了加、减、乘、除的运算。
```
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator implements ActionListener {
JFrame frame;
JTextField textField;
JButton button0, button1, button2, button3, button4, button5, button6, button7, button8, button9, buttonAdd, buttonSub, buttonMul, buttonDiv, buttonClr, buttonEql;
int num1, num2, result;
char operator;
Calculator() {
frame = new JFrame("Calculator");
frame.setLayout(new GridLayout(5, 4));
textField = new JTextField();
textField.setEditable(false);
frame.add(textField);
button1 = new JButton("1");
frame.add(button1);
button1.addActionListener(this);
button2 = new JButton("2");
frame.add(button2);
button2.addActionListener(this);
button3 = new JButton("3");
frame.add(button3);
button3.addActionListener(this);
buttonAdd = new JButton("+");
frame.add(buttonAdd);
buttonAdd.addActionListener(this);
button4 = new JButton("4");
frame.add(button4);
button4.addActionListener(this);
button5 = new JButton("5");
frame.add(button5);
button5.addActionListener(this);
button6 = new JButton("6");
frame.add(button6);
button6.addActionListener(this);
buttonSub = new JButton("-");
frame.add(buttonSub);
buttonSub.addActionListener(this);
button7 = new JButton("7");
frame.add(button7);
button7.addActionListener(this);
button8 = new JButton("8");
frame.add(button8);
button8.addActionListener(this);
button9 = new JButton("9");
frame.add(button9);
button9.addActionListener(this);
buttonMul = new JButton("*");
frame.add(buttonMul);
buttonMul.addActionListener(this);
buttonClr = new JButton("C");
frame.add(buttonClr);
buttonClr.addActionListener(this);
button0 = new JButton("0");
frame.add(button0);
button0.addActionListener(this);
buttonEql = new JButton("=");
frame.add(buttonEql);
buttonEql.addActionListener(this);
buttonDiv = new JButton("/");
frame.add(buttonDiv);
buttonDiv.addActionListener(this);
frame.pack();
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button1) {
textField.setText(textField.getText().concat("1"));
}
if (e.getSource() == button2) {
textField.setText(textField.getText().concat("2"));
}
if (e.getSource() == button3) {
textField.setText(textField.getText().concat("3"));
}
if (e.getSource() == button4) {
textField.setText(textField.getText().concat("4"));
}
if (e.getSource() == button5) {
textField.setText(textField.getText().concat("5"));
}
if (e.getSource() == button6) {
textField.setText(textField.getText().concat("6"));
}
if (e.getSource() == button7) {
textField.setText(textField.getText().concat("7"));
}
if (e.getSource() == button8) {
textField.setText(textField.getText().concat("8"));
}
if (e.getSource() == button9) {
textField.setText(textField.getText().concat("9"));
}
if (e.getSource() == button0) {
textField.setText(textField.getText().concat("0"));
}
if (e.getSource() == buttonClr) {
textField.setText("");
}
if (e.getSource() == buttonAdd) {
num1 = Integer.parseInt(textField.getText());
operator = '+';
textField.setText("");
}
if (e.getSource() == buttonSub) {
num1 = Integer.parseInt(textField.getText());
operator = '-';
textField.setText("");
}
if (e.getSource() == buttonMul) {
num1 = Integer.parseInt(textField.getText());
operator = '*';
textField.setText("");
}
if (e.getSource() == buttonDiv) {
num1 = Integer.parseInt(textField.getText());
operator = '/';
textField.setText("");
}
if (e.getSource() == buttonEql) {
num2 = Integer.parseInt(textField.getText());
switch (operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num1 / num2;
break;
}
textField.setText(String.valueOf(result));
}
}
public static void main(String[] args) {
new Calculator();
}
}
```
在这个程序中,我们创建了一个JFrame窗口和一个JTextField用于显示用户输入和计算结果。我们还创建了数字按钮、运算符按钮和一个清除按钮,并使用addActionListener()方法为每个按钮添加了一个ActionListener。
在ActionListener中,我们使用getSource()方法确定哪个按钮被单击。如果数字按钮被单击,我们将数字添加到文本字段中。如果运算符按钮被单击,我们将当前数字存储在num1变量中,并将运算符存储在operator变量中,并清空文本字段。如果等号按钮被单击,我们将当前数字存储在num2变量中,并根据存储在operator变量中的运算符执行相应的运算。最后,我们将计算结果显示在文本字段中。
请注意,这个程序没有进行错误处理。例如,如果用户在第二个数字之前单击了等号按钮,则会出现NumberFormatException异常。你可以通过添加适当的错误处理来改善程序。