java设计和编写一个简易的计算器:实现计算器的界面;实现计算按钮不同的计算功能;显示计算器按钮运算后的结果。
时间: 2023-11-27 17:50:06 浏览: 113
这里提供一个简单的Java Swing实现的计算器示例:
```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, buttonEqual, buttonClear;
int num1=0, num2=0, result=0;
char operator;
Calculator() {
frame = new JFrame("计算器");
frame.setLayout(new GridLayout(5, 4));
textField = new JTextField();
textField.setEditable(false);
button0 = new JButton("0");
button1 = new JButton("1");
button2 = new JButton("2");
button3 = new JButton("3");
button4 = new JButton("4");
button5 = new JButton("5");
button6 = new JButton("6");
button7 = new JButton("7");
button8 = new JButton("8");
button9 = new JButton("9");
buttonAdd = new JButton("+");
buttonSub = new JButton("-");
buttonMul = new JButton("*");
buttonDiv = new JButton("/");
buttonEqual = new JButton("=");
buttonClear = new JButton("C");
button0.addActionListener(this);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
button6.addActionListener(this);
button7.addActionListener(this);
button8.addActionListener(this);
button9.addActionListener(this);
buttonAdd.addActionListener(this);
buttonSub.addActionListener(this);
buttonMul.addActionListener(this);
buttonDiv.addActionListener(this);
buttonEqual.addActionListener(this);
buttonClear.addActionListener(this);
frame.add(textField);
frame.add(buttonClear);
frame.add(buttonDiv);
frame.add(buttonMul);
frame.add(button7);
frame.add(button8);
frame.add(button9);
frame.add(buttonSub);
frame.add(button4);
frame.add(button5);
frame.add(button6);
frame.add(buttonAdd);
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.add(buttonEqual);
frame.add(button0);
frame.setSize(300, 300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button0) {
textField.setText(textField.getText() + "0");
}
if (e.getSource() == button1) {
textField.setText(textField.getText() + "1");
}
if (e.getSource() == button2) {
textField.setText(textField.getText() + "2");
}
if (e.getSource() == button3) {
textField.setText(textField.getText() + "3");
}
if (e.getSource() == button4) {
textField.setText(textField.getText() + "4");
}
if (e.getSource() == button5) {
textField.setText(textField.getText() + "5");
}
if (e.getSource() == button6) {
textField.setText(textField.getText() + "6");
}
if (e.getSource() == button7) {
textField.setText(textField.getText() + "7");
}
if (e.getSource() == button8) {
textField.setText(textField.getText() + "8");
}
if (e.getSource() == button9) {
textField.setText(textField.getText() + "9");
}
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() == buttonEqual) {
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));
}
if (e.getSource() == buttonClear) {
textField.setText("");
}
}
public static void main(String[] args) {
new Calculator();
}
}
```
这个计算器界面由一个文本框和16个按钮组成,其中数字按钮用于输入数字,四个运算符按钮用于选择运算符,等号按钮用于计算结果,清除按钮用于清除文本框内容。在按钮的 ActionListener 中,根据不同的按钮执行相应的操作,具体实现方式参见代码注释。
阅读全文