Java gui计算器实现加减乘除和求余等功能
时间: 2023-06-27 11:07:20 浏览: 103
以下是一个简单的Java GUI计算器实现加减乘除和求余等功能的示例代码:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener {
private JTextField textField;
private JButton button0, button1, button2, button3, button4, button5, button6, button7, button8, button9;
private JButton buttonAdd, buttonSubtract, buttonMultiply, buttonDivide, buttonModulus, buttonEqual, buttonClear;
private double num1, num2, result;
private String operator;
public Calculator() {
super("Calculator");
// Create text field
textField = new JTextField(20);
textField.setEditable(false);
// Create number buttons
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");
// Create operator buttons
buttonAdd = new JButton("+");
buttonSubtract = new JButton("-");
buttonMultiply = new JButton("*");
buttonDivide = new JButton("/");
buttonModulus = new JButton("%");
buttonEqual = new JButton("=");
buttonClear = new JButton("C");
// Add action listeners to buttons
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);
buttonSubtract.addActionListener(this);
buttonMultiply.addActionListener(this);
buttonDivide.addActionListener(this);
buttonModulus.addActionListener(this);
buttonEqual.addActionListener(this);
buttonClear.addActionListener(this);
// Create number panel and add buttons
JPanel numberPanel = new JPanel(new GridLayout(4, 3));
numberPanel.add(button1);
numberPanel.add(button2);
numberPanel.add(button3);
numberPanel.add(button4);
numberPanel.add(button5);
numberPanel.add(button6);
numberPanel.add(button7);
numberPanel.add(button8);
numberPanel.add(button9);
numberPanel.add(buttonClear);
numberPanel.add(button0);
numberPanel.add(buttonEqual);
// Create operator panel and add buttons
JPanel operatorPanel = new JPanel(new GridLayout(3, 2));
operatorPanel.add(buttonAdd);
operatorPanel.add(buttonSubtract);
operatorPanel.add(buttonMultiply);
operatorPanel.add(buttonDivide);
operatorPanel.add(buttonModulus);
// Create content pane and add text field and panels
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(textField, BorderLayout.NORTH);
contentPane.add(numberPanel, BorderLayout.CENTER);
contentPane.add(operatorPanel, BorderLayout.EAST);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button0) {
textField.setText(textField.getText() + "0");
} else if (e.getSource() == button1) {
textField.setText(textField.getText() + "1");
} else if (e.getSource() == button2) {
textField.setText(textField.getText() + "2");
} else if (e.getSource() == button3) {
textField.setText(textField.getText() + "3");
} else if (e.getSource() == button4) {
textField.setText(textField.getText() + "4");
} else if (e.getSource() == button5) {
textField.setText(textField.getText() + "5");
} else if (e.getSource() == button6) {
textField.setText(textField.getText() + "6");
} else if (e.getSource() == button7) {
textField.setText(textField.getText() + "7");
} else if (e.getSource() == button8) {
textField.setText(textField.getText() + "8");
} else if (e.getSource() == button9) {
textField.setText(textField.getText() + "9");
} else if (e.getSource() == buttonAdd) {
num1 = Double.parseDouble(textField.getText());
operator = "+";
textField.setText("");
} else if (e.getSource() == buttonSubtract) {
num1 = Double.parseDouble(textField.getText());
operator = "-";
textField.setText("");
} else if (e.getSource() == buttonMultiply) {
num1 = Double.parseDouble(textField.getText());
operator = "*";
textField.setText("");
} else if (e.getSource() == buttonDivide) {
num1 = Double.parseDouble(textField.getText());
operator = "/";
textField.setText("");
} else if (e.getSource() == buttonModulus) {
num1 = Double.parseDouble(textField.getText());
operator = "%";
textField.setText("");
} else if (e.getSource() == buttonEqual) {
num2 = Double.parseDouble(textField.getText());
if (operator.equals("+")) {
result = num1 + num2;
} else if (operator.equals("-")) {
result = num1 - num2;
} else if (operator.equals("*")) {
result = num1 * num2;
} else if (operator.equals("/")) {
result = num1 / num2;
} else if (operator.equals("%")) {
result = num1 % num2;
}
textField.setText(String.valueOf(result));
} else if (e.getSource() == buttonClear) {
textField.setText("");
}
}
public static void main(String[] args) {
Calculator calculator = new Calculator();
calculator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
calculator.pack();
calculator.setLocationRelativeTo(null);
calculator.setVisible(true);
}
}
```
这个程序使用了Java的Swing GUI工具包来创建用户界面,并使用了ActionListener接口来处理按钮事件。当用户点击数字和运算符按钮时,它们的文本将添加到文本字段中。当用户点击等号按钮时,程序将解析文本字段中的数字并执行所选操作,并将结果显示在文本字段中。当用户点击清除按钮时,文本字段将被清空。
阅读全文