java设计简单温度转化器(摄转华,华转摄)

该Java程序创建了一个图形用户界面,用于进行摄氏度和华氏度之间的转换。用户输入温度值,点击按钮进行转换,结果会显示在文本框中。程序还包括清除功能,可清空输入和结果。
摘要由CSDN通过智能技术生成

// 导入需要使用的 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();
    }
}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值