java 华氏温度和摄氏温度转换问题

boofuu 2006-06-09 09:27:31
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//将这个例子讲述JButton,JTextField,JLabel的用法
public class CelsiusConverter implements ActionListener {
JFrame converterFrame;
JPanel converterPaanel;
JTexField tempCelsius;
JLabel celsiusLabel,fahrenheitLabel;
JButton convertTemp;
//构造函数
public CelsiusConverter() {
//创建容器
converterFrame = new JFrame("温度转换器");
converterFrame.setSize(40,40);
converterJPanel = new JPanel();
converterJPanel.setLayout(new GridLayout(2,2));
//增加widgets
addWidgets();
//向frame中添加panel
converterFrame.getContentPane().add(converterPanel,BorderLayout.CENTER);
//关闭窗口时退出
converterFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//显示转换器
converterFrame.pack();
converterFrame.setVisible(true);
}
//为转换器创建和增加widgets
private void addWidgets(){
//创建widgets.
tempCelsius = new JTextField(2);
celsiusLabel = new JLabel("摄氏温度",SwingConstants.LEFT);
convertTemp = new JButton("转换……");
fahrenheitLabel = new JLabel("华氏温度",SwingConstants.LTFT);
//诊听转换器按钮发出的事件
convertTemp.addActionListener(this);
//向容器中添加widgets
converterPanel.add(tempCelsius);
converterPanel.add(celsiusLabel);
converterPanel.add(convertTemp);
converterPanel.add(fahrenheitLabel);
celsiusLabel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
fahrenheitLabel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
}
//实现ActionListener接口
public void actionPerformed(ActionEvent event) {
//将摄氏温度转换为双精度小数,并且转换为华氏温度
int tempFahr =
(int)((Double.parseDouble(tempCelsius.getText())) * 1.8 + 32);
fahrenheitLabel.setText(tempFahr + " Fahrenheit");
}
//main 方法
public static void main(String[] args)
{
//异常处理
try
{
UIManager.setLookAndFeel(
UIManager.getCrossPlatformLookAndFeelClassName());
}
catch(Exception e){}
CelsiusConverter converter = new CelsiusConverter();
}
}

运行时有9个错误……哪位好心人帮我调试一些啊……
...全文
1081 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
joyofly 2006-06-11
  • 打赏
  • 举报
回复
呵呵,新手上路写了一段Application~

import java.io.*;
public class A {
public static void main(String[] args) throws IOException {
String s=null;
double sheshi=0;
double huashi;
System.out.println("请输入摄氏温度");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try{
s = br.readLine();
sheshi = Double.parseDouble(s);
}catch(NumberFormatException e){
System.out.println("请输入一个双精度值");
}
huashi = (9 * sheshi/5) + 2;
System.out.println(huashi);
}
}

不行,困了,先发这些了
boofuu 2006-06-09
  • 打赏
  • 举报
回复
难道全部去看世界杯了……怎么没人理的…………世界杯!!~~~
boofuu 2006-06-09
  • 打赏
  • 举报
回复
无与伦比兄还在嘛?
boofuu 2006-06-09
  • 打赏
  • 举报
回复
不知道(无与伦比)有什么好的方法呢?可以共享嘛?
BST_2010 2006-06-09
  • 打赏
  • 举报
回复
没有必要如此复杂?/

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧