用java怎么写
时间: 2023-07-05 09:13:52 浏览: 84
如果你想在Java中实现一个计算器,可以通过创建一个图形用户界面(GUI)来实现用户输入和输出的交互效果。下面是一个简单的Java GUI计算器示例:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener {
private JTextField displayField;
private JButton button0, button1, button2, button3, button4,
button5, button6, button7, button8, button9,
buttonAdd, buttonSub, buttonMul, buttonDiv, buttonEq,
buttonClear, buttonDot;
double num1, num2, result;
int operator;
public Calculator() {
super("Calculator");
setLayout(new BorderLayout());
//创建显示面板
JPanel displayPanel = new JPanel();
displayField = new JTextField(20);
displayField.setEditable(false);
displayField.setHorizontalAlignment(JTextField.RIGHT);
displayPanel.add(displayField);
//创建按钮面板
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4, 4));
//创建数字按钮
button7 = new JButton("7");
button7.addActionListener(this);
buttonPanel.add(button7);
button8 = new JButton("8");
button8.addActionListener(this);
buttonPanel.add(button8);
button9 = new JButton("9");
button9.addActionListener(this);
buttonPanel.add(button9);
buttonDiv = new JButton("/");
buttonDiv.addActionListener(this);
buttonPanel.add(buttonDiv);
button4 = new JButton("4");
button4.addActionListener(this);
buttonPanel.add(button4);
button5 = new JButton("5");
button5.addActionListener(this);
buttonPanel.add(button5);
button6 = new JButton("6");
button6.addActionListener(this);
buttonPanel.add(button6);
buttonMul = new JButton("*");
buttonMul.addActionListener(this);
buttonPanel.add(buttonMul);
button1 = new JButton("1");
button1.addActionListener(this);
buttonPanel.add(button1);
button2 = new JButton("2");
button2.addActionListener(this);
buttonPanel.add(button2);
button3 = new JButton("3");
button3.addActionListener(this);
buttonPanel.add(button3);
buttonSub = new JButton("-");
buttonSub.addActionListener(this);
buttonPanel.add(buttonSub);
button0 = new JButton("0");
button0.addActionListener(this);
buttonPanel.add(button0);
buttonDot = new JButton(".");
buttonDot.addActionListener(this);
buttonPanel.add(buttonDot);
buttonEq = new JButton("=");
buttonEq.addActionListener(this);
buttonPanel.add(buttonEq);
buttonAdd = new JButton("+");
buttonAdd.addActionListener(this);
buttonPanel.add(buttonAdd);
buttonClear = new JButton("C");
buttonClear.addActionListener(this);
buttonPanel.add(buttonClear);
//将面板添加到窗体中
add(displayPanel, BorderLayout.NORTH);
add(buttonPanel, BorderLayout.CENTER);
//设置窗体属性
setSize(300, 300);
setLocationRelativeTo(null);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button0) {
displayField.setText(displayField.getText() + "0");
} else if (e.getSource() == button1) {
displayField.setText(displayField.getText() + "1");
} else if (e.getSource() == button2) {
displayField.setText(displayField.getText() + "2");
} else if (e.getSource() == button3) {
displayField.setText(displayField.getText() + "3");
} else if (e.getSource() == button4) {
displayField.setText(displayField.getText() + "4");
} else if (e.getSource() == button5) {
displayField.setText(displayField.getText() + "5");
} else if (e.getSource() == button6) {
displayField.setText(displayField.getText() + "6");
} else if (e.getSource() == button7) {
displayField.setText(displayField.getText() + "7");
} else if (e.getSource() == button8) {
displayField.setText(displayField.getText() + "8");
} else if (e.getSource() == button9) {
displayField.setText(displayField.getText() + "9");
} else if (e.getSource() == buttonDot) {
displayField.setText(displayField.getText() + ".");
} else if (e.getSource() == buttonAdd) {
num1 = Double.parseDouble(displayField.getText());
operator = 1;
displayField.setText("");
} else if (e.getSource() == buttonSub) {
num1 = Double.parseDouble(displayField.getText());
operator = 2;
displayField.setText("");
} else if (e.getSource() == buttonMul) {
num1 = Double.parseDouble(displayField.getText());
operator = 3;
displayField.setText("");
} else if (e.getSource() == buttonDiv) {
num1 = Double.parseDouble(displayField.getText());
operator = 4;
displayField.setText("");
} else if (e.getSource() == buttonClear) {
displayField.setText("");
} else if (e.getSource() == buttonEq) {
num2 = Double.parseDouble(displayField.getText());
switch (operator) {
case 1:
result = num1 + num2;
break;
case 2:
result = num1 - num2;
break;
case 3:
result = num1 * num2;
break;
case 4:
result = num1 / num2;
break;
default:
break;
}
displayField.setText(String.valueOf(result));
}
}
public static void main(String[] args) {
new Calculator();
}
}
```
这个示例中创建了一个JFrame窗体,并添加了一个JTextField文本框用于显示计算结果,以及一组JButton按钮用于输入数字和操作符。当用户点击数字按钮时,程序会将数字添加到文本框中;当用户点击操作符按钮时,程序会记录当前的数字以及操作符;当用户点击等号按钮时,程序会根据当前的数字和操作符计算结果并显示在文本框中。
阅读全文