2014-01-22 3 views
0

Так что я должен сделать эту программу GUI, которая будет одновременно конвертировать температуры (Цельсия, Фаренгейта и Кельвина). проблема - каждый раз, когда я запускаю программу, она не позволит мне редактировать JTextFields. Я думаю, что проблема находится где-то здесь, в JLabels и ActionListener:Преобразователь температуры GUI

//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

Смотрите также [ 'Converter'] (http://docs.oracle.com/javase/tutorial/uiswing/components/panel.html#eg). – trashgod

ответ

0

Вы можете использовать два Differents текстовые поля для ввода «Цельсий», и вы сделал ошибку, используя их, попробуйте следующее:

Celsius = celInput; // input initialized at the beginnning of your class 
Смежные вопросы