GUI温度转换器

时间:2014-01-22 22:57:39

标签: java swing user-interface actionlistener jtextfield

所以我必须制作这个同时转换温度(摄氏度,华氏度和开尔文度)的GUI程序。问题是我每次运行程序时都不会让我编辑JTextFields。我认为问题出在Jlabels和actionListener中的某处:

    //create and initialize JLabels here
    Celsius = new JTextField("0", 5);
    Fahrenheit = new JTextField("32", 4);
    Kelvin = new JTextField("273.15", 5);

    JLabel celLabel = new JLabel("Celsius:");
    JLabel fahLabel = new JLabel("Fahrenheit:");
    JLabel kelLabel = new JLabel("Kelvin:");

    EventHandler listener = new EventHandler();
    Celsius.addActionListener(listener);
    Fahrenheit.addActionListener(listener);
    Kelvin.addActionListener(listener);

如果有帮助,这是我的完整代码:

public class M4L1T1 {
    static JTextField celInput = new JTextField("0");
    static JTextField Fahrenheit, Celsius, Kelvin;


    private static class EventHandler implements ActionListener {
        /**
         * this method gets called when an object we are listening to is interacted with
         *
         * @param evt    ActionEvent that interacted with
         */
        public void actionPerformed(ActionEvent evt) {
            //creates the formating we would like for the numbers
            DecimalFormat df = new DecimalFormat("#.##");
            //if the event triggered was celInput than
            if (evt.getSource() == celInput) {

                try {

                   //get the input from the JTextField
                   String num = Celsius.getText();

                   //convert the String to a number                    
                  double number = Double.parseDouble(num);

                  //set the JTextFields to the formated number of the converted temps
                 String fNum = df.format(convertCtoF(number));
                 String kNum = df.format(convertCtoK(number));

                 Fahrenheit.setText(fNum);
                 Kelvin.setText(kNum);

               } catch (NumberFormatException e) {
                   //this happens if Java CANNOT convert the String to a number
                   celInput.setText("Illegal data");
               }
             }
        else if (evt.getSource() == celInput) {

            try {

                //get the input from the JTextField
               String num = Fahrenheit.getText();

                //convert the String to a number                    
              double number = Double.parseDouble(num);

                //set the JTextFields to the formated number of the converted temps
              String cNum = df.format(convertFtoC(number));
              String kNum = df.format(convertCtoK(convertFtoC(number)));


              Celsius.setText(cNum);
              Kelvin.setText(kNum);

            } catch (NumberFormatException e) {
                //this happens if Java CANNOT convert the String to a number
                celInput.setText("Illegal data");
            }
          }
        else {

            try {                 

                 //get the input from the JTextField
               String num = Kelvin.getText();

                 //convert the String to a number                    
                double number = Double.parseDouble(num);


                //set the JTextFields to the formated number of the converted temps
                String fNum = df.format(convertCtoF(convertKtoC(number)));
                String cNum = df.format(convertKtoC(number));

                Fahrenheit.setText(fNum);
                Celsius.setText(cNum);

            } catch (NumberFormatException e) {
                //this happens if Java CANNOT convert the String to a number
                celInput.setText("Illegal data");
            }
        }           

    }//end actionPerformed method

    /**
     * ...
     *
     * @param c     Degrees Celsius
     * @return f    Degrees Fahrenheit
     */
    private double convertCtoF (double c) {
        double f = c * 9/5 + 32;
        return f;
    } //end convertCtoF method   

    /**
     * ...
     *
     * @param c     Degrees Celsius
     * @return k    Kelvin
     */
    private double convertCtoK (double c) {
        double k = c + 273.15;
        return k;
    } //end convertCtoK method

    /**
     * ...
     *
     * @param f     Degrees Fahrenheit
     * @return c    Degrees Celsius
     */
    private double convertFtoC (double f) {
        double c =  (f - 32) * 5/9;
        return f;
    } //end convertFtoC method

    /**
     * ...
     *
     * @param k     Kelvin
     * @return c    Degrees Celsius
     */
    private double convertKtoC (double k) {
        double c = k - 273.15;
        return c;
    } //end convertTtoC method
}

public static void main(String args[]) {
    //creates a new window
    JFrame window = new JFrame("Temperature Conversion");

    //create JPanels here
    JPanel main = new JPanel();
    JPanel xPanel = new JPanel();
    JPanel yPanel = new JPanel();
    JPanel zPanel = new JPanel();
    JPanel ansPanel = new JPanel();


    main.add(xPanel);
    main.add(yPanel);
    main.add(zPanel);
    main.add(ansPanel);


    //create and initialize JLabels here
    Celsius = new JTextField("0", 5);
    Fahrenheit = new JTextField("32", 4);
    Kelvin = new JTextField("273.15", 5);

    JLabel celLabel = new JLabel("Celsius:");
    JLabel fahLabel = new JLabel("Fahrenheit:");
    JLabel kelLabel = new JLabel("Kelvin:");

    EventHandler listener = new EventHandler();
    Celsius.addActionListener(listener);
    Fahrenheit.addActionListener(listener);
    Kelvin.addActionListener(listener);

    main.setLayout(new GridLayout(3,1));
    xPanel.add(celLabel);
    xPanel.add(Celsius);

    yPanel.add(fahLabel);
    yPanel.add(Fahrenheit);

    zPanel.add(kelLabel);
    zPanel.add(Kelvin);



    //paints the main panel to the jframe and
    //displays the jframe
    window.setContentPane(main);
    window.setSize(250,110);
    window.setLocation(100,100);
    window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    window.setVisible(true);
  }//end main method
} //end class

1 个答案:

答案 0 :(得分:0)

您为“摄氏”输入使用两个不同的文本字段,并且使用它们时出错,请尝试:

Celsius = celInput; // input initialized at the beginnning of your class