package 界面; import java.awt.*; import java.awt.event.*; import javax.swing.*; class SimpleComputeDemo4 { JTextField firstTF,secondTF,resultTF; JButton SZHButton,HZSButton,clearButton; public void init() { JFrame f=new JFrame("温度转换"); f.setSize(400,200); f.setVisible(true); JPanel p=new JPanel(); JLabel firstLabel=new JLabel("摄氏度"); JLabel secondLabel=new JLabel("华氏度"); JLabel thirdLabel=new JLabel("结果"); firstTF=new JTextField(10); secondTF=new JTextField(10); resultTF=new JTextField(10); SZHButton=new JButton("摄转华"); HZSButton=new JButton("华转摄"); clearButton=new JButton("清除"); f.add(p); p.setLayout(null); firstLabel.setBounds(20,10,80,30); firstTF.setBounds(120,10,120,30); secondLabel.setBounds(20,50,80,30); secondTF.setBounds(120,50,120,30); thirdLabel.setBounds(20,90,80,30); resultTF.setBounds(120,90,120,30); SZHButton.setBounds(20,170,80,30 ); HZSButton.setBounds(120,170,80,30); clearButton.setBounds(220,170,60,30); p.add(firstLabel); p.add(firstTF); p.add(secondLabel); p.add(secondTF); p.add(thirdLabel); p.add(resultTF); p.add(SZHButton); p.add(HZSButton); p.add(clearButton); SZHButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { double first,second,result=0; first=Double.parseDouble(firstTF.getText()); second=Double.parseDouble(secondTF.getText()); result=9*first/5+32; resultTF.setText(String.valueOf(result)); } }); HZSButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { double first,second,result=0; first=Double.parseDouble(firstTF.getText()); second=Double.parseDouble(secondTF.getText()); result=5*(second-32)/9; resultTF.setText(String.valueOf(result)); } }); clearButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { firstTF.setText(null); secondTF.setText(null); resultTF.setText(null); } }); } } class TestSimpleComputeDemo { public static void main(String args[]) { new SimpleComputeDemo4().init(); } }给这段代码加详细的注释
时间: 2024-01-10 19:03:25 浏览: 29
下面是对这段代码的详细注释:
```java
// 导入需要使用的 Java 类库
package 界面;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
// SimpleComputeDemo4 类
class SimpleComputeDemo4 {
// 声明需要使用的组件
JTextField firstTF,secondTF,resultTF; // 文本框组件
JButton SZHButton,HZSButton,clearButton; // 按钮组件
// 初始化界面方法
public void init() {
// 创建 JFrame 窗口对象
JFrame f=new JFrame("温度转换");
// 设置窗口大小
f.setSize(400,200);
// 设置窗口可见
f.setVisible(true);
// 创建 JPanel 面板对象
JPanel p=new JPanel();
// 创建标签组件
JLabel firstLabel=new JLabel("摄氏度");
JLabel secondLabel=new JLabel("华氏度");
JLabel thirdLabel=new JLabel("结果");
// 创建文本框组件
firstTF=new JTextField(10);
secondTF=new JTextField(10);
resultTF=new JTextField(10);
// 创建按钮组件
SZHButton=new JButton("摄转华");
HZSButton=new JButton("华转摄");
clearButton=new JButton("清除");
// 将面板添加到窗口
f.add(p);
// 设置面板布局为 null
p.setLayout(null);
// 设置标签、文本框、按钮的位置和大小
firstLabel.setBounds(20,10,80,30);
firstTF.setBounds(120,10,120,30);
secondLabel.setBounds(20,50,80,30);
secondTF.setBounds(120,50,120,30);
thirdLabel.setBounds(20,90,80,30);
resultTF.setBounds(120,90,120,30);
SZHButton.setBounds(20,170,80,30);
HZSButton.setBounds(120,170,80,30);
clearButton.setBounds(220,170,60,30);
// 将标签、文本框、按钮添加到面板
p.add(firstLabel);
p.add(firstTF);
p.add(secondLabel);
p.add(secondTF);
p.add(thirdLabel);
p.add(resultTF);
p.add(SZHButton);
p.add(HZSButton);
p.add(clearButton);
// 为摄转华按钮添加事件监听器
SZHButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// 获取摄氏度和华氏度的值
double first,second,result=0;
first=Double.parseDouble(firstTF.getText());
second=Double.parseDouble(secondTF.getText());
// 摄氏度转华氏度的计算
result=9*first/5+32;
// 将计算结果设置到结果文本框中
resultTF.setText(String.valueOf(result));
}
});
// 为华转摄按钮添加事件监听器
HZSButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// 获取摄氏度和华氏度的值
double first,second,result=0;
first=Double.parseDouble(firstTF.getText());
second=Double.parseDouble(secondTF.getText());
// 华氏度转摄氏度的计算
result=5*(second-32)/9;
// 将计算结果设置到结果文本框中
resultTF.setText(String.valueOf(result));
}
});
// 为清除按钮添加事件监听器
clearButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 清空文本框中的内容
firstTF.setText(null);
secondTF.setText(null);
resultTF.setText(null);
}
});
}
}
// TestSimpleComputeDemo 类
class TestSimpleComputeDemo {
public static void main(String args[]) {
// 创建 SimpleComputeDemo4 对象并调用 init() 方法初始化界面
new SimpleComputeDemo4().init();
}
}
```