用Java编写一个具备加减乘除的计算器界面代码
时间: 2024-05-08 09:22:19 浏览: 86
java加减乘除计算器界面编程
5星 · 资源好评率100%
以下是使用Java Swing编写的简单计算器界面代码,实现了加减乘除四则运算:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener {
private JButton button0, button1, button2, button3, button4, button5, button6, button7, button8, button9;
private JButton buttonAdd, buttonSubtract, buttonMultiply, buttonDivide, buttonClear, buttonEquals;
private JTextField textField;
private double firstValue = 0;
private double secondValue = 0;
private String operation = "";
public Calculator() {
super("Calculator");
// Set up the text field
textField = new JTextField(10);
textField.setEditable(false);
textField.setHorizontalAlignment(JTextField.RIGHT);
// Set up the 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");
buttonAdd = new JButton("+");
buttonSubtract = new JButton("-");
buttonMultiply = new JButton("*");
buttonDivide = new JButton("/");
buttonClear = new JButton("C");
buttonEquals = new JButton("=");
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);
buttonClear.addActionListener(this);
buttonEquals.addActionListener(this);
// Set up the content pane
JPanel contentPane = new JPanel();
contentPane.setLayout(new BorderLayout());
contentPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4, 4, 5, 5));
buttonPanel.add(button7);
buttonPanel.add(button8);
buttonPanel.add(button9);
buttonPanel.add(buttonDivide);
buttonPanel.add(button4);
buttonPanel.add(button5);
buttonPanel.add(button6);
buttonPanel.add(buttonMultiply);
buttonPanel.add(button1);
buttonPanel.add(button2);
buttonPanel.add(button3);
buttonPanel.add(buttonSubtract);
buttonPanel.add(button0);
buttonPanel.add(buttonClear);
buttonPanel.add(buttonEquals);
buttonPanel.add(buttonAdd);
contentPane.add(textField, BorderLayout.NORTH);
contentPane.add(buttonPanel, BorderLayout.CENTER);
setContentPane(contentPane);
pack();
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
}
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) {
firstValue = Double.parseDouble(textField.getText());
operation = "+";
textField.setText("");
} else if (e.getSource() == buttonSubtract) {
firstValue = Double.parseDouble(textField.getText());
operation = "-";
textField.setText("");
} else if (e.getSource() == buttonMultiply) {
firstValue = Double.parseDouble(textField.getText());
operation = "*";
textField.setText("");
} else if (e.getSource() == buttonDivide) {
firstValue = Double.parseDouble(textField.getText());
operation = "/";
textField.setText("");
} else if (e.getSource() == buttonClear) {
textField.setText("");
} else if (e.getSource() == buttonEquals) {
secondValue = Double.parseDouble(textField.getText());
double result = 0;
if (operation.equals("+")) {
result = firstValue + secondValue;
} else if (operation.equals("-")) {
result = firstValue - secondValue;
} else if (operation.equals("*")) {
result = firstValue * secondValue;
} else if (operation.equals("/")) {
result = firstValue / secondValue;
}
textField.setText(Double.toString(result));
}
}
public static void main(String[] args) {
Calculator calculator = new Calculator();
calculator.setVisible(true);
}
}
```
阅读全文