所以我的代码要求用户输入。然后转换温度。应该很简单,但我的代码不起作用。它不输出我的标签 3 它什么也不做。这是我的代码唯一的问题,我只是不知道如何解决它
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);
}
}