Hello. I am trying to recreate manually a small celsius-to-fahrenheit converter that I created with Design view before that. I used its code for reference, but still doesn't work. When I run my code I don't get the components drawn on the screen, only an empty window. What am I missing, probably the type of layout?
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class CelsiusConverterCoded extends JFrame
{
private JTextField temptext;
private JLabel fLabel;
private JLabel label;
private JButton button;
public CelsiusConverterCoded()
{
initComponents();
}
public void initComponents()
{
JFrame frame = new JFrame("Celsius Converter");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
temptext = new JTextField("Enter degrees hmnya");
frame.getContentPane().add(temptext);
label = new JLabel("Celsius");
frame.getContentPane().add(label);
button = new JButton();
button.setText("Convert");
button.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent evt)
{
convertButtonActionPerformed(evt);
}
});
fLabel = new JLabel();
frame.getContentPane().add(fLabel);
frame.pack();
}
private void convertButtonActionPerformed(ActionEvent evt)
{
int tempFahr = (int)((Double.parseDouble(temptext.getText()))*1.8+32);
fLabel.setText(tempFahr+" Fahrenheit");
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
@Override
public void run()
{
new CelsiusConverterCoded().setVisible(true);
}
});
}
}