摄氏温度到华氏度转换器GUI程序
问题描述:
我无法弄清楚为什么我的代码不工作。我对GUI编程和Java也很陌生,在创建GUI程序的格式上我仍然有点粗糙。在代码中,我试图将摄氏温度转换为华氏温度,反之亦然。任何帮助,将不胜感激。谢谢!摄氏温度到华氏度转换器GUI程序
import java.awt.Container;
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.JTextField;
@SuppressWarnings("serial")
public class myGUIClass<FahrenheitButtonHandler> extends JFrame{
private JLabel msgCelsius;
private JLabel msgFahrenheit;
private JButton btnCelsius;
private JButton btnFahrenheit;
private static JTextField fldCelsius;
private static JTextField fldFahrenheit;
Container contain;
public myGUIClass(String myGUIWindow){
super("myGui");
contain = getContentPane();
contain.setLayout(new FlowLayout());
msgCelsius = new JLabel("Degrees in Celsius");
btnCelsius = new JButton("Convert From Celsius to Fahrenheit");
fldCelsius = new JTextField(15);
msgFahrenheit = new JLabel("Degrees in Fahrenheit ");
btnFahrenheit = new JButton("Convert From Fahrenheit to Celsius");
fldFahrenheit = new JTextField(15);
contain.add(msgCelsius);
contain.add(fldCelsius);
contain.add(btnCelsius);
contain.add(msgFahrenheit);
contain.add(fldFahrenheit);
contain.add(btnFahrenheit);
CelsiusButtonHandler btnHandlerCelsius = new CelsiusButtonHandler();
btnCelsius.addActionListener(btnHandlerCelsius);
FahrenheitButtonHandler btnHandlerFahrenheit = new FahrenheitButtonHandler();
btnFahrenheit.addActionListener(btnHandlerFahrenheit);
setSize(400,200);
setVisible(true);
}//end method
private class CelsiusButtonHandler implements ActionListener{
//@Override
//implement the listener interface methods to process the events
public void actionPerformed(ActionEvent ae){
Integer celsius;
Integer fahrenheit;
try{
if (ae.getSource() == btnCelsius){
celsius = Integer.parseInt(fldCelsius.getText());
fahrenheit = Math.round((9 /(float)5)) * (celsius + 32);
fldFahrenheit.setText(fahrenheit.toString());
}//end if
}//end try
catch (Exception e){
fldFahrenheit.setText("");
}//end catch
}//end inner class
}//end class
private class FahrenheitButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent a){
Integer fahrenheit1;
Integer celsius1;
try{
if(a.getSource()== btnFahrenheit){
fahrenheit1 = Integer.parseInt(fldFahrenheit.getText());
celsius1 = Math.round((5/(float)9)) * (fahrenheit1 - 32);
fldCelsius.setText(celsius1.toString());
}//end if
}//end try
catch (Exception e){
fldCelsius.setText("");
}//end catch
}//end method
}//end private class
public static void main (String[] args){
@SuppressWarnings("rawtypes")
myGUIClass guiClass = new myGUIClass(null);
guiClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}//end main
}//end outer class
//theres a problem with the math in these lines:
//am i not casting these correctly? whenever i input 50 i'm supposed to get 122 but i get 164.
//fahrenheit = Math.round((9 /(float)5)) * (celsius + 32);
//celsius1 = Math.round((5/(float)9)) * (fahrenheit1 - 32);
答
忽略代码问题,使uncompliable的例子...
你有一个整数除法问题...
celsius = (5/9) * (fahrenheit - 32);
如果5/9
= 0
所得到的值转换为一个整数。
尝试使用更多的东西一样......
celsius = Math.round((5/(float)9)) * (fahrenheit - 32);
现在,就个人而言,我会用double
或float
而不是int
和格式化的结果,但就是我。你需要做同样的事情FahrenheitButtonHandler
在你FahrenheitButtonHandler
类,你也将错误值文本字段...
celsius1 = Integer.parseInt(fldFahrenheit.getText());
fahrenheit1 = celsius1*(9/5)+32;
fldCelsius.setText(celsius1.toString());
您应用celsius1
值,这是值从fldFahrenheit
领域的精华,而不是计算结果,应该是
fldCelsius.setText(fahrenheit1.toString());
...但是请记住,还有你需要纠正这种情况的整数除法问题... ...
最后,你注册了错误的ActionListener
到btnFahrenheit
FahrenheitButtonHandler btnHandlerFahrenheit = new FahrenheitButtonHandler();
btnFahrenheit.addActionListener(btnHandlerCelsius); // <-- Wrong listener...
它应该是...
FahrenheitButtonHandler btnHandlerFahrenheit = new FahrenheitButtonHandler();
btnFahrenheit.addActionListener(btnHandlerFahrenheit);
而问题是....? – MadProgrammer 2014-11-05 23:37:55
当我运行它并尝试输入华氏温度时,它不会在摄氏度的文本字段中显示任何内容。相反的问题相反 – ekep23 2014-11-05 23:42:05
究竟是什么问题?它不转换吗?它不会编译?为什么你的类在声明中有一个通用的,为什么你在事后抑制原始类型的警告? – Zymus 2014-11-05 23:42:14