按要求编写一个Swing程序,并编译、运行这个程序,要求:当单击“求和”按钮时,把“和”显示在“求和”按钮后的文本行中,当单击“清除”按钮后,3个文本行的内容全部被清除。
时间: 2023-09-14 18:09:20 浏览: 45
以下是一个实现您要求的Swing程序示例:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SumCalculator extends JFrame implements ActionListener {
private JTextField number1Field, number2Field, sumField;
private JButton sumButton, clearButton;
public SumCalculator() {
setTitle("Sum Calculator");
setSize(300, 150);
setLayout(new GridLayout(4, 2));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel number1Label = new JLabel("Number 1:");
number1Field = new JTextField();
add(number1Label);
add(number1Field);
JLabel number2Label = new JLabel("Number 2:");
number2Field = new JTextField();
add(number2Label);
add(number2Field);
JLabel sumLabel = new JLabel("Sum:");
sumField = new JTextField();
sumField.setEditable(false);
add(sumLabel);
add(sumField);
sumButton = new JButton("求和");
sumButton.addActionListener(this);
add(sumButton);
clearButton = new JButton("清除");
clearButton.addActionListener(this);
add(clearButton);
setVisible(true);
}
public static void main(String[] args) {
new SumCalculator();
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == sumButton) {
try {
int num1 = Integer.parseInt(number1Field.getText());
int num2 = Integer.parseInt(number2Field.getText());
int sum = num1 + num2;
sumField.setText(Integer.toString(sum));
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(this, "请输入有效的数字");
}
} else if (e.getSource() == clearButton) {
number1Field.setText("");
number2Field.setText("");
sumField.setText("");
}
}
}
```
在这个程序中,我们创建了一个名为 `SumCalculator` 的Swing窗口,并在其中添加了3个文本行和2个按钮,用于计算两个数字的和并清除所有文本行的内容。当用户单击“求和”按钮时,程序将从文本行中获取两个数字,并将它们相加后显示在“和”文本行中。如果用户输入的不是有效的数字,则会显示一个弹出框提示。当用户单击“清除”按钮时,程序将清除所有文本行的内容。
要编译和运行这个程序,您可以按照以下步骤操作:
1. 将上面的代码保存到名为 `SumCalculator.java` 的文件中。
2. 打开命令行窗口或终端,并导航到保存 `SumCalculator.java` 文件的目录。
3. 编译程序,输入以下命令并按回车键:
```
javac SumCalculator.java
```
4. 运行程序,输入以下命令并按回车键:
```
java SumCalculator
```
5. 程序将打开一个Swing窗口,您可以在其中输入数字并单击“求和”按钮以计算它们的和,或单击“清除”按钮以清除所有文本行的内容。
希望这个示例能够帮助您编写和运行自己的Swing程序。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)