0

I have the following GUI code but my JLabel for results doesn't work when either of the buttons are pushed

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TempConverterPanel extends JPanel
{

   private JLabel input, results;
   private JButton output_f, output_c;
   private JTextField temp, fahrenheit, celsius;

   public TempConverterPanel()
   {
		setLayout (new GridLayout (3, 1));
		setLayout (new FlowLayout ());
		setPreferredSize (new Dimension(400, 175));
      setBackground (Color.red);


	//asks for the temp input in fahrenheit

      input = new JLabel ("Enter the temperature you want to convert:");
       	   output_f = new JButton ("Convert to Celsius: ");
	output_c = new JButton ("Convert to Fahrenheit: ");
  results = new JLabel ("Input converted to is: ");
                
                temp = new JTextField (6);
		output_f.addActionListener (new FahrenheitListener());
		output_c.addActionListener (new CelsiusListener());

              
      add (input);
      add (temp);
      add (output_f);
		add (output_c);
      add (results);

 
   }


  //sets the listener for the fahrenheit temp

   private class FahrenheitListener implements ActionListener
   {

    public void actionPerformed (ActionEvent event)
      {
         double fahrenheitTemp, celsiusTemp;

         String text = temp.getText();

         fahrenheitTemp = Double.parseDouble(text);
         celsiusTemp = (fahrenheitTemp - 32) * 5/9;

         results.setText (Double.toString (celsiusTemp));

      }
   }


//sets the listener for the celsius temp

   private class CelsiusListener implements ActionListener
   {

    public void actionPerformed (ActionEvent event)
      {
         double celsiusTemp, fahrenheitTemp;

         String text = temp.getText();

         celsiusTemp = Double.parseDouble (text);
         fahrenheitTemp = celsiusTemp *  9/5 + 32;

         results.setText (Double.toString (fahrenheitTemp));

      }
   }
}

is there anyway i can fix this problem

3
Contributors
8
Replies
11
Views
8 Years
Discussion Span
Last Post by dnmoore
Featured Replies
  • 1
    Ezzaral 2,714   8 Years Ago

    Well of course it does. You set the text to that number. If you want that label to remain the same then use a separate label for the converted value. Read More

0

I tried your code by running it (with a few changes like extending JFrame instead of JPanel, adding a main to make it run as a stand alone), and it worked.
Its converting the temperature correctly when I click on the buttons also.

So whats the problem ????

0

the jlabel displays for results but when i push the buttons the results label disappears.....

0

It works just fine for me as well. The only "disappearing" I could see is that the label will move depending on the length of the value displayed in the "result". That is because you used FlowLayout.

0

this is what my main looks like

import javax.swing.JFrame;

public class Temp2
{
  
  public static void main (String[] args)
   {
      JFrame frame = new JFrame ("Temp2");
      frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

      TempConverterPanel panel = new TempConverterPanel();

      frame.getContentPane().add(panel);
      frame.pack();
      frame.setVisible(true);
   }
}
0

i get the same thing when i run my main program the results label is there but as soon as i hit the converter buttons the label disppears and the only thing left is the converted number......

1

Well of course it does. You set the text to that number. If you want that label to remain the same then use a separate label for the converted value.

Votes + Comments
Oh dear never could have imagined that was his problem
0

thank oyu i fixed it it works the way its suppose to, i appreciate everyones help.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.