练习swing布局和事件布局中计算器的界面和简单功能
时间: 2024-02-17 19:05:09 浏览: 61
好的,我可以帮你实现一个基于Swing的简单计算器界面和功能。下面是实现步骤:
1. 创建一个基于JFrame的窗口,并设置窗口标题和大小。
```
import javax.swing.*;
public class Calculator extends JFrame {
public static void main(String[] args) {
Calculator calculator = new Calculator();
}
public Calculator() {
setTitle("Calculator");
setSize(300, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
}
```
2. 在窗口中创建一个JPanel,并使用GridLayout布局,将JPanel分为5行4列,每个单元格大小相等。
```
import javax.swing.*;
import java.awt.*;
public class Calculator extends JFrame {
public static void main(String[] args) {
Calculator calculator = new Calculator();
}
public Calculator() {
setTitle("Calculator");
setSize(300, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridLayout(5, 4));
add(panel);
setVisible(true);
}
}
```
3. 在JPanel中添加JButton,并设置文本和字体。
```
import javax.swing.*;
import java.awt.*;
public class Calculator extends JFrame {
public static void main(String[] args) {
Calculator calculator = new Calculator();
}
public Calculator() {
setTitle("Calculator");
setSize(300, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridLayout(5, 4));
add(panel);
JButton button1 = new JButton("7");
button1.setFont(new Font("Arial", Font.PLAIN, 20));
panel.add(button1);
JButton button2 = new JButton("8");
button2.setFont(new Font("Arial", Font.PLAIN, 20));
panel.add(button2);
// 添加其它按钮
setVisible(true);
}
}
```
4. 使用BorderLayout布局,在窗口底部添加一个JTextField,用于显示计算结果。
```
import javax.swing.*;
import java.awt.*;
public class Calculator extends JFrame {
private JTextField resultField = new JTextField();
public static void main(String[] args) {
Calculator calculator = new Calculator();
}
public Calculator() {
setTitle("Calculator");
setSize(300, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridLayout(5, 4));
add(panel, BorderLayout.CENTER);
JButton button1 = new JButton("7");
button1.setFont(new Font("Arial", Font.PLAIN, 20));
panel.add(button1);
JButton button2 = new JButton("8");
button2.setFont(new Font("Arial", Font.PLAIN, 20));
panel.add(button2);
// 添加其它按钮
add(resultField, BorderLayout.SOUTH);
setVisible(true);
}
}
```
5. 添加事件监听器,实现计算器的功能。这里仅实现加法功能。可以在每个按钮上添加相应的ActionListener,也可以使用一个统一的ActionListener。
```
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Calculator extends JFrame {
private JTextField resultField = new JTextField();
private double num1 = 0;
private double num2 = 0;
private String operator = "+";
public static void main(String[] args) {
Calculator calculator = new Calculator();
}
public Calculator() {
setTitle("Calculator");
setSize(300, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridLayout(5, 4));
add(panel, BorderLayout.CENTER);
JButton button1 = new JButton("7");
button1.setFont(new Font("Arial", Font.PLAIN, 20));
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
resultField.setText(resultField.getText() + "7");
}
});
panel.add(button1);
JButton button2 = new JButton("8");
button2.setFont(new Font("Arial", Font.PLAIN, 20));
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
resultField.setText(resultField.getText() + "8");
}
});
panel.add(button2);
JButton addButton = new JButton("+");
addButton.setFont(new Font("Arial", Font.PLAIN, 20));
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
num1 = Double.parseDouble(resultField.getText());
operator = "+";
resultField.setText("");
}
});
panel.add(addButton);
JButton calculateButton = new JButton("=");
calculateButton.setFont(new Font("Arial", Font.PLAIN, 20));
calculateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
num2 = Double.parseDouble(resultField.getText());
switch (operator) {
case "+":
resultField.setText(String.valueOf(num1 + num2));
break;
}
}
});
panel.add(calculateButton);
add(resultField, BorderLayout.SOUTH);
setVisible(true);
}
}
```
这样,一个简单的计算器界面和加法功能就实现了。你可以根据需要添加其它功能,例如减法、乘法、除法等。
阅读全文