请你用Java编写以上程序代码
时间: 2024-03-10 09:47:55 浏览: 14
好的,以下是使用Java编写的代码:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MultiplicationAndDivision extends JFrame implements ActionListener {
private JLabel num1Label, num2Label, resultLabel;
private JTextField num1Field, num2Field, resultField;
private JButton multiplyBtn, divideBtn;
public MultiplicationAndDivision() {
super("乘除运算");
setLayout(new GridLayout(4, 2, 10, 10)); // 设置布局
// 添加标签和输入框
num1Label = new JLabel("第一个数:");
add(num1Label);
num1Field = new JTextField(10);
add(num1Field);
num2Label = new JLabel("第二个数:");
add(num2Label);
num2Field = new JTextField(10);
add(num2Field);
// 添加乘法和除法按钮
multiplyBtn = new JButton("乘法");
multiplyBtn.addActionListener(this);
add(multiplyBtn);
divideBtn = new JButton("除法");
divideBtn.addActionListener(this);
add(divideBtn);
// 添加结果标签和输入框
resultLabel = new JLabel("结果:");
add(resultLabel);
resultField = new JTextField(10);
resultField.setEditable(false);
add(resultField);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 关闭窗口时退出程序
setSize(300, 200);
setLocationRelativeTo(null); // 设置窗口居中显示
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == multiplyBtn) {
try {
int num1 = Integer.parseInt(num1Field.getText());
int num2 = Integer.parseInt(num2Field.getText());
resultField.setText(String.valueOf(num1 * num2));
} catch (NumberFormatException ex) {
resultField.setText("异常提示:请输入整数!");
}
} else if (e.getSource() == divideBtn) {
try {
int num1 = Integer.parseInt(num1Field.getText());
int num2 = Integer.parseInt(num2Field.getText());
if (num2 == 0) {
resultField.setText("异常提示:除数不能为0!");
} else {
resultField.setText(String.valueOf(num1 / num2));
}
} catch (NumberFormatException ex) {
resultField.setText("异常提示:请输入整数!");
}
}
}
public static void main(String[] args) {
new MultiplicationAndDivision();
}
}
```
这段代码使用了Java Swing库中的JFrame、JLabel、JTextField、JButton等组件,实现了窗口的创建、标签和输入框的添加、按钮的添加和事件绑定、异常处理等功能。在按钮的事件处理方法中,我们使用try-catch方法来捕获可能出现的异常,如输入的不是整数或除数为0等情况,在出现异常时,会给出相应的错误提示。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)