用java编写一个简易的计算器,要求能进行加减乘除运算,通过鼠标点击按键交互,并使用边界布局,还要有归零按键
时间: 2023-06-20 13:09:23 浏览: 97
以下是一个简单的Java计算器程序,实现了加减乘除和归零功能,并使用了边界布局。
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener {
private JButton btn0, btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9;
private JButton btnAdd, btnSub, btnMul, btnDiv, btnClear, btnEquals;
private JTextField txtResult;
private double num1, num2;
private String op = "";
public Calculator() {
super("Calculator");
// create buttons
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("/");
btnClear = new JButton("C");
btnEquals = new JButton("=");
// create text field
txtResult = new JTextField(20);
txtResult.setEditable(false);
// set layout
setLayout(new BorderLayout());
// add components to the frame
JPanel panel1 = new JPanel();
panel1.setLayout(new GridLayout(4, 3));
panel1.add(btn1);
panel1.add(btn2);
panel1.add(btn3);
panel1.add(btn4);
panel1.add(btn5);
panel1.add(btn6);
panel1.add(btn7);
panel1.add(btn8);
panel1.add(btn9);
panel1.add(btnClear);
panel1.add(btn0);
panel1.add(btnEquals);
JPanel panel2 = new JPanel();
panel2.setLayout(new GridLayout(4, 1));
panel2.add(btnAdd);
panel2.add(btnSub);
panel2.add(btnMul);
panel2.add(btnDiv);
JPanel panel3 = new JPanel();
panel3.setLayout(new FlowLayout());
panel3.add(txtResult);
add(panel1, BorderLayout.CENTER);
add(panel2, BorderLayout.EAST);
add(panel3, BorderLayout.NORTH);
// add action listeners to the buttons
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);
btnClear.addActionListener(this);
btnEquals.addActionListener(this);
// set frame properties
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(250, 250);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String button = e.getActionCommand();
if (button.equals("C")) { // clear
txtResult.setText("");
num1 = 0;
num2 = 0;
op = "";
} else if (button.equals("+")) { // addition
num1 = Double.parseDouble(txtResult.getText());
op = "+";
txtResult.setText("");
} else if (button.equals("-")) { // subtraction
num1 = Double.parseDouble(txtResult.getText());
op = "-";
txtResult.setText("");
} else if (button.equals("*")) { // multiplication
num1 = Double.parseDouble(txtResult.getText());
op = "*";
txtResult.setText("");
} else if (button.equals("/")) { // division
num1 = Double.parseDouble(txtResult.getText());
op = "/";
txtResult.setText("");
} else if (button.equals("=")) { // equals
num2 = Double.parseDouble(txtResult.getText());
if (op.equals("+")) {
txtResult.setText(Double.toString(num1 + num2));
} else if (op.equals("-")) {
txtResult.setText(Double.toString(num1 - num2));
} else if (op.equals("*")) {
txtResult.setText(Double.toString(num1 * num2));
} else if (op.equals("/")) {
txtResult.setText(Double.toString(num1 / num2));
}
} else { // digit
txtResult.setText(txtResult.getText() + button);
}
}
public static void main(String[] args) {
new Calculator();
}
}
```
运行此程序,将会弹出一个简易的计算器界面。
阅读全文