Question

I have a task to create a Fahrenheit to Celsius converter, I've written the program as follows, it's the way that teacher has taught us, so I'm obliged to follow it, I know it's even very rudimentary but I have no other choice to follow his stupid way.

All I need help with is that I want to replace that JOptionPane in the last line with "JLabel", I want the result to be showed in a JLabel. What can I do?

Converter.java

public class Converter extends JFrame implements ActionListener{
JTextField Textbox = new JTextField (20);
JButton button1 = new JButton("Submit");
int x;

public Converter()
{
    setLayout(new FlowLayout());
    add(button1);
    add(Textbox);
    button1.addActionListener(this);
    setVisible(true);
    setSize(800,600);
}
public void actionPerformed(ActionEvent arg0)
{
    String s=Textbox.getText();
    int res=Integer.parseInt(s);
    JOptionPane.showMessageDialog(null,(res-32)*(5)/(9));  ///I need help here
}
}

WindowFrame.java

public class WindowFrame {
public static void main(String[] args) {
    Converter x = new Converter();
}
}
Was it helpful?

Solution

Create a new JLabel then add it to your container (JFrame), then in the actionPerformed you can do:

public void actionPerformed(ActionEvent arg0)
{
    String s = Textbox.getText();
    int res = Integer.parseInt(s);
    String result = String.valueOf((res-32)*(5)/(9));
    label.setText(result);
}

OTHER TIPS

Do the changes in Converter.java as given in image,

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top