Java Swing 计算器实现 - 简单图形界面编程
Java Swing 计算器实现 - 简单图形界面编程
本教程将带您一步步使用 Java Swing 框架编写一个简单的计算器程序。我们将使用 FlowLayout 布局管理器,创建按钮和文本框,并实现基本加减乘除运算功能。
步骤 1:创建界面
首先,我们创建一个 JFrame 窗口,并设置标题为 '计算'。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame implements ActionListener {
// ...
public Calculator() {
setTitle('计算');
setLayout(new FlowLayout());
// ...
}
// ...
}
步骤 2:添加按钮和文本框
接下来,我们创建四个按钮:'加'、'差'、'积'、'除',以及三个文本框:两个用于输入数字,一个用于显示结果。
// ...
public Calculator() {
// ...
addButton = new JButton('加');
subButton = new JButton('差');
mulButton = new JButton('积');
divButton = new JButton('除');
inputField1 = new JTextField(10);
inputField2 = new JTextField(10);
resultField = new JTextField(10);
resultField.setEditable(false);
add(new JLabel('数字1:'));
add(inputField1);
add(new JLabel('数字2:'));
add(inputField2);
add(new JLabel('结果:'));
add(resultField);
add(addButton);
add(subButton);
add(mulButton);
add(divButton);
// ...
}
// ...
}
步骤 3:添加事件监听器
为了响应按钮点击事件,我们需要添加事件监听器。
// ...
public Calculator() {
// ...
addButton.addActionListener(this);
subButton.addActionListener(this);
mulButton.addActionListener(this);
divButton.addActionListener(this);
// ...
}
// ...
}
步骤 4:实现计算逻辑
在 actionPerformed 方法中,我们根据点击的按钮类型,执行相应的计算逻辑。
// ...
public void actionPerformed(ActionEvent e) {
int num1 = Integer.parseInt(inputField1.getText());
int num2 = Integer.parseInt(inputField2.getText());
if (e.getSource() == addButton) {
resultField.setText('' + (num1 + num2));
} else if (e.getSource() == subButton) {
resultField.setText('' + (num1 - num2));
} else if (e.getSource() == mulButton) {
resultField.setText('' + (num1 * num2));
} else if (e.getSource() == divButton) {
if (num2 == 0) {
resultField.setText('除数不能为0');
} else {
resultField.setText('' + (num1 / num2));
}
}
}
// ...
}
步骤 5:运行程序
最后,我们创建 Calculator 对象,并使其可见。
// ...
public static void main(String[] args) {
new Calculator();
}
}
完整代码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame implements ActionListener {
private JButton addButton, subButton, mulButton, divButton;
private JTextField inputField1, inputField2, resultField;
public Calculator() {
setTitle('计算');
setLayout(new FlowLayout());
addButton = new JButton('加');
subButton = new JButton('差');
mulButton = new JButton('积');
divButton = new JButton('除');
inputField1 = new JTextField(10);
inputField2 = new JTextField(10);
resultField = new JTextField(10);
resultField.setEditable(false);
add(new JLabel('数字1:'));
add(inputField1);
add(new JLabel('数字2:'));
add(inputField2);
add(new JLabel('结果:'));
add(resultField);
add(addButton);
add(subButton);
add(mulButton);
add(divButton);
addButton.addActionListener(this);
subButton.addActionListener(this);
mulButton.addActionListener(this);
divButton.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
int num1 = Integer.parseInt(inputField1.getText());
int num2 = Integer.parseInt(inputField2.getText());
if (e.getSource() == addButton) {
resultField.setText('' + (num1 + num2));
} else if (e.getSource() == subButton) {
resultField.setText('' + (num1 - num2));
} else if (e.getSource() == mulButton) {
resultField.setText('' + (num1 * num2));
} else if (e.getSource() == divButton) {
if (num2 == 0) {
resultField.setText('除数不能为0');
} else {
resultField.setText('' + (num1 / num2));
}
}
}
public static void main(String[] args) {
new Calculator();
}
}
通过运行这段代码,您将看到一个简单的计算器窗口。您可以输入两个数字,点击按钮进行计算,结果将显示在第三个文本框中。
注意:
- 该计算器只支持整数运算。
- 除法运算中,除数不能为 0。
您可以根据需要扩展该计算器,例如添加更多功能,例如浮点数运算、记忆功能、清除功能等。
原文地址: https://www.cveoy.top/t/topic/oMuJ 著作权归作者所有。请勿转载和采集!