所以我的代碼要求用戶輸入。然後轉換溫度。應該很簡單,但我的代碼不起作用。它不輸出我的標籤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);
}
}
對代碼塊使用一致的邏輯縮進。代碼的縮進旨在幫助人們理解程序流程! – 2013-05-03 15:09:13