2013-05-03 79 views
0

所以我的代碼要求用戶輸入。然後轉換溫度。應該很簡單,但我的代碼不起作用。它不輸出我的標籤3它只是沒有做任何事情。這是我與我的代碼具有唯一的問題,我只是不知道如何解決它JLabel不在GUI中顯示文本

import javax.swing.*; 


    public class FahrenheitPanel extends JPanel 

    { 

private JLabel lable1; 
private JLabel lable2; 
private JLabel lable3; 
private JTextField fahrenheit; 
    public FahrenheitPanel() 

    { 

    lable1 = new JLabel ("Enter Fahrenheit temperature:"); 

    lable2 = new JLabel ("Temperature in Celsius: "); 

    fahrenheit = new JTextField (5); 

    fahrenheit.addActionListener (new TempListener()); 
    add (lable1); 
    add (fahrenheit); 
    add (lable2);  


    setPreferredSize (new Dimension(300, 75)); 

} 

private class TempListener implements ActionListener 

{ 
    public void actionPerformed (ActionEvent event) 

    { 

    int fahrenheitTemp, celsiusTemp; 

    String text = fahrenheit.getText(); 
    fahrenheitTemp = Integer.parseInt (text); 

    celsiusTemp = (fahrenheitTemp-32) * 5/9; 
    lable3.setText(Integer.toString (celsiusTemp)); 
     add (lable3);       
    } 
} 


    public static void main (String[] args) 
    { 
    JFrame frame = new JFrame ("Fahrenheit to Celsius Converter"); 

    frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); 
    FahrenheitPanel panel = new FahrenheitPanel(); 
    frame.getContentPane().add(panel); 
    frame.pack(); 
    frame.setVisible(true); 
    } 
    } 
+2

對代碼塊使用一致的邏輯縮進。代碼的縮進旨在幫助人們理解程序流程! – 2013-05-03 15:09:13

回答

2

不要在actionPerformed(...)方法labl3的JLabel添加到您的GUI因爲這樣做,這意味着你將試圖添加JLabel的次數與偵聽器方法的調用次數相同,並且需要調用重新驗證並重新進行不必要的重新繪製。相反,在類的構造函數中從一開始就將這個JLabel添加到您的GUI中。

+1

@ ser2264202使用JFormattedTextField和數字格式程序,而不是普通的JTextField – mKorbel 2013-05-03 15:30:18

2

第三個標籤應該從一開始就添加到框架中,並帶有一些默認文本。

如果您動態添加標籤,則必須驗證容器(通過在面板上調用validate())。

此外,您不應該設置面板的首選大小。佈局管理器根據其包含的組件計算首選大小。

2

首先,我將整數值轉換爲雙精度,因爲它可能有雙精度數字, 其次,您的類沒有在您的類中添加標籤,這是問題...運行您的程序,如果有任何問題,請隨時問我

import java.awt.Dimension; 
    import java.awt.event.ActionEvent; 
    import java.awt.event.ActionListener; 

    import javax.swing.*; 


    public class FahrenheitPanel extends JPanel 

     { 

      private JLabel lable1; 
      private JLabel lable2; 
      private JLabel lable3; 
      private JTextField fahrenheit; 

    public FahrenheitPanel() 

     { 

      lable1 = new JLabel ("Enter Fahrenheit temperature:"); 

     lable2 = new JLabel ("Temperature in Celsius: "); 

     lable3 = new JLabel(""); 
     fahrenheit = new JTextField (5); 

     fahrenheit.addActionListener ((ActionListener) new TempListener()); 
     add (lable1); 
     add (fahrenheit); 
     add (lable2);  
     add(lable3); 

     setPreferredSize (new Dimension(250, 75)); 

     } 

    private class TempListener implements ActionListener 

     { 
      public void actionPerformed (ActionEvent event) 

       { 

        double fahrenheitTemp, celsiusTemp; 

        String text = fahrenheit.getText(); 
        fahrenheitTemp = Double.parseDouble (text); 

        celsiusTemp = ((fahrenheitTemp-32) * 5/9); 
        lable3.setText(Double.toString (celsiusTemp)); 

       } 
     } 


public static void main (String[] args) 
{ 
     JFrame frame = new JFrame ("Fahrenheit to Celsius Converter"); 

     frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); 
     FahrenheitPanel panel = new FahrenheitPanel(); 
     frame.getContentPane().add(panel); 
     frame.pack(); 
     frame.setVisible(true); 
} 
}