Celsius到Fahrenheit转换器GUI程序

时间:2014-11-05 23:36:55

标签: java swing user-interface jframe

我无法弄清楚为什么我的代码不起作用的逻辑。我也是GUI编程和java的新手,我对创建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);

1 个答案:

答案 0 :(得分:1)

忽略使示例不可兑换的代码问题......

你有一个整数除法问题......

celsius = (5 / 9) * (fahrenheit - 32);

如果5/9 = 0,则结果值将转换为整数。

尝试使用更像......

的内容
celsius = Math.round((5 / (float)9)) * (fahrenheit - 32);

现在,就个人而言,我会使用doublefloat代替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);