日期:2014-05-20  浏览次数:20923 次

做了好久都没做出来,请求帮忙!
编写一个将华氏温度转换为摄氏温度的程序,通过一个文本框输入华氏温度,能过一个标签输出相应的摄氏温度,公式是:摄氏温度=5/9( 华氏温度-32)

------解决方案--------------------
提取文本框的内容,转换成数字,然后计算,呵呵。。。
------解决方案--------------------
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Temperature extends JFrame implements ActionListener
{
JTextField text;
JLabel label;

public Temperature(String s)
{
super(s);
setBounds(100, 100, 160, 100);
Container con = getContentPane();
con.setLayout(new FlowLayout());

text = new JTextField(8);
label = new JLabel("摄氏温度: ");
text.addActionListener(this);

con.add(text);
con.add(label);
con.validate();
}

public void actionPerformed(ActionEvent e)
{
String ss = text.getText();
try
{
int a = (Integer.valueOf(ss) - 32) * 5 / 9;
label.setText("摄氏温度: " + a);
}
catch(Exception ee){label.setText("请输入整数");}
}

public static void main(String[] args)
{
Temperature t = new Temperature("温度");
t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
t.setVisible(true);
}
}
------解决方案--------------------
在计算公式处 楼主可以修改!

Java code

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * @author bzwm
 * 
 */
public class ConvFahrenheit {
    public static void main(String[] args) {
        new FahrenToCentigrade();
    }
}

class FahrenToCentigrade {
    private JFrame f = null;

    private JPanel p = null;

    private JLabel fl = null;

    private JTextField ft = null;

    private JLabel cl = null;

    private JTextField ct = null;

    private JButton b = null;

    public FahrenToCentigrade() {
        f = new JFrame("ConvFahrenheit");
        p = new JPanel(new FlowLayout(FlowLayout.LEFT));
        fl = new JLabel("ConvFahrenheit");

        ft = new JTextField(5);

        cl = new JLabel("centigrade");

        ct = new JTextField(15);
        ct.setEditable(false);
        b = new JButton("Conv");
        b.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                String fahren = ft.getText();
                if (fahren.matches("[+-]?\\d+(.\\d+)?")) {
                    if (Float.parseFloat(fahren) - 32 == 0)
                        try {
                            throw new java.lang.ArithmeticException("/ by zero");
                        } catch (Exception e) {
                            javax.swing.JOptionPane.showMessageDialog(null, "input value is illegal[/ by zero]");
                            e.printStackTrace();
                        }
                        //计算公式
                    ct.setText(String.valueOf((5.0 / 9.0 * (Float.parseFloat(fahren) - 32.0))));
                }
                else{
                    javax.swing.JOptionPane.showMessageDialog(null, "input value is illegal");
                }
            }
            
        });
        p.add(fl);
        p.add(ft);
        p.add(cl);
        p.add(ct);
        p.add(b);
        f.getContentPane().add(p, java.awt.BorderLayout.CENTER);
        f.setSize(300, 300);
        f.setLocation(100, 100);
        f.pack();
        f.setVisible(true);
    }
}

------解决方案--------------------
Java code

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.math.BigDecimal; 

public class Demo extends JFrame implements ActionListener{
    
    private JLabel lab1=new JLabel("华氏温度");
    private JLabel lab2=new JLabel("摄氏温度");
    private JTextField t1=new JTextField(10);  //用来输入并显示华氏温度
    private JTextField t2=new JTextField(10);  //用来显示摄氏温度
    private JButton sure=new JButton("转换");  
    private JButton reset=new JButton("重置");
    private String temStr1=null;  //用来存储华氏温度的字符串
    
    public Demo(){
        
        t2.setEditable(false);  //设为不可编辑状态
        sure.addActionListener(this);  //注册给本类监听器
        reset.addActionListener(this);
        
        Container c = this.getContentPane();
        c.setLayout(new FlowLayout(FlowLayout.CENTER,20,30));  //采用流式布局
        
        //添加所有组件
        c.add(lab1);
        c.add(t1);
        c.add(sure);
        c.add(lab2);
        c.add(t2);
        c.add(reset);    
            
        this.setTitle("温度转换");
        this.setBounds(450,100,300,200);  //设置主窗口显示的位置以及大小
        this.setResizable(false);  //主窗口大小固定,不可改变
        this.setVisible(true);  //设为可见
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  //退出处理
        
    }
    
    public void actionPerformed(ActionEvent e){ //处理事件
        if(e.getSource()==sure){  //当点击“转换”按钮时
            double tem1,tem2;
            try{
                temStr1=t1.getText().trim();  //获取华氏温度的字符串
                if(temStr1==null||temStr1.equals(""))
                    throw new NumberFormatException();    //获取华氏温度的值                
                tem1=Double.parseDouble(temStr1);
                tem2=5*(tem1-32)/9;  //转换为摄氏温度
                
                //四舍五入并保留小数后的前两位
                t2.setText(String.valueOf((new BigDecimal(tem2)).setScale(2,BigDecimal.ROUND_HALF_UP))); 
            }catch(NumberFormatException nfc){    //捕捉数据转换异常
                JOptionPane.showMessageDialog(this,"请输入正确的华氏温度!","转换错误",JOptionPane.WARNING_MESSAGE);
                t1.setText("");
                return ;
            }catch(Exception exc){
                System.exit(0);
            }
        }
        
        else if(e.getSource()==reset){  //当点击“重置”按钮时
            //全部设为初始状态
            temStr1=null;
            t1.setText("");
            t2.setText("");
        }
    }
    public static void main(String args[]){
        Demo mainFrame = new Demo();
    }
}