实验目的:练习swing布局和事件处理 。实验要求:通过java制作计算器的界面和简单功能
时间: 2024-02-17 12:05:13 浏览: 68
好的,这个实验要求你使用Java Swing布局和事件处理来制作一个简单的计算器。下面是一个简单的示例代码,演示了如何创建计算器的界面和实现加法操作:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame implements ActionListener {
private JTextField textField;
private JButton button1, button2, button3, button4, button5, button6, button7, button8, button9, button0;
private JButton buttonAdd, buttonMinus, buttonMultiply, buttonDivide, buttonEqual, buttonClear;
private double firstNumber = 0;
private String operator = "";
public Calculator() {
setTitle("Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 300);
setResizable(false);
setLocationRelativeTo(null);
textField = new JTextField();
textField.setEditable(false);
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");
button0 = new JButton("0");
buttonAdd = new JButton("+");
buttonMinus = new JButton("-");
buttonMultiply = new JButton("*");
buttonDivide = new JButton("/");
buttonEqual = new JButton("=");
buttonClear = new JButton("C");
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);
button0.addActionListener(this);
buttonAdd.addActionListener(this);
buttonMinus.addActionListener(this);
buttonMultiply.addActionListener(this);
buttonDivide.addActionListener(this);
buttonEqual.addActionListener(this);
buttonClear.addActionListener(this);
JPanel panel1 = new JPanel(new GridLayout(4, 3));
panel1.add(button1);
panel1.add(button2);
panel1.add(button3);
panel1.add(button4);
panel1.add(button5);
panel1.add(button6);
panel1.add(button7);
panel1.add(button8);
panel1.add(button9);
panel1.add(button0);
panel1.add(buttonClear);
panel1.add(buttonEqual);
JPanel panel2 = new JPanel(new GridLayout(4, 1));
panel2.add(buttonAdd);
panel2.add(buttonMinus);
panel2.add(buttonMultiply);
panel2.add(buttonDivide);
JPanel panel3 = new JPanel(new GridLayout(2, 1));
panel3.add(textField);
panel3.add(panel2);
setLayout(new BorderLayout());
add(panel1, BorderLayout.CENTER);
add(panel3, BorderLayout.EAST);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == buttonClear) {
textField.setText("");
firstNumber = 0;
operator = "";
} else if (e.getSource() == buttonAdd) {
firstNumber = Double.parseDouble(textField.getText());
textField.setText("");
operator = "+";
} else if (e.getSource() == buttonMinus) {
firstNumber = Double.parseDouble(textField.getText());
textField.setText("");
operator = "-";
} else if (e.getSource() == buttonMultiply) {
firstNumber = Double.parseDouble(textField.getText());
textField.setText("");
operator = "*";
} else if (e.getSource() == buttonDivide) {
firstNumber = Double.parseDouble(textField.getText());
textField.setText("");
operator = "/";
} else if (e.getSource() == buttonEqual) {
double secondNumber = Double.parseDouble(textField.getText());
double result = 0;
if (operator.equals("+")) {
result = firstNumber + secondNumber;
} else if (operator.equals("-")) {
result = firstNumber - secondNumber;
} else if (operator.equals("*")) {
result = firstNumber * secondNumber;
} else if (operator.equals("/")) {
result = firstNumber / secondNumber;
}
textField.setText(String.valueOf(result));
firstNumber = 0;
operator = "";
} else {
textField.setText(textField.getText() + e.getActionCommand());
}
}
public static void main(String[] args) {
new Calculator();
}
}
```
这个代码创建了一个名为Calculator的类,继承了JFrame类,并实现了ActionListener接口。它创建了一个文本框和一些按钮,并使用GridLayout和BorderLayout来布局它们。它还实现了ActionListener接口的actionPerformed方法,以响应按钮的单击事件。通过这个程序,你可以练习使用Swing布局和事件处理来制作计算器这样的简单应用程序。
阅读全文