I need a little help converting my GUI panel into JButtons instead of text for some parts

//Davina Moore 
//TempConverterPanel
//November 6, 2008

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

public class TempConverterPanel extends JPanel
{
   private JLabel input, results_f, results_c;
	private JButton output;
   private JTextField fahrenheit, celsius;

   public TempConverterPanel()
   {
	
	//asks for the temp input in fahrenheit
	
      input = new JLabel ("Enter the Fahrenheit Temperature:");
	   output = new JButton ("Convert to Celsius: ");
      results_f = new JLabel (" ");
		
      fahrenheit = new JTextField (10);
      fahrenheit.addActionListener (new TempFListener());

      add (input);
      add (fahrenheit);
      add (output);
      add (results_f);
		
	//asks for the temp input in celsius
		
		input = new JLabel ("Enter the Celsius Temperature:");
      output = new JButton ("Convert to Fahrenheit: ");
		results_c = new JLabel (" ");

      celsius = new JTextField (10);
      celsius.addActionListener (new TempCListener());

      add (input);
      add (celsius);
      add (output);
      add (results_c);
		
      setPreferredSize (new Dimension(400, 175));
      setBackground (Color.red);
		setLayout (new GridLayout (0, 1));
   }


  //sets the listener for the fahrenheit temp
  
   private class TempFListener implements ActionListener
   {

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

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

         results_f.setText (Double.toString (celsiusTemp));
 
      }
   }
	
	
//sets the listener for the celsius temp

   private class TempCListener implements ActionListener
   {

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

         String text = celsius.getText();

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

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

      }
   }
}

when i run it, it ask me to enter the temp in fahrenheit and celsius and then its suppose to push the button to get the conversion but my button doesnt work and the only way to get the converted temp is to press enter on keyboard, also i have it where you enter the temp separate but your only suppose to enter 1 temp and then push the button for Fahrenheit or Celsius
what are some ideas on how to fix and change my panel to work the right way
* it works just not the way its suppose to*
if anyone can help the help will be appreciated
thanks so much for your time and guidance

Recommended Answers

All 2 Replies

Your button "output" doesn't work cause you haven't added an ActionListener for it like your JTextField

Also you have just one JButton object named "output" and you are first initializing here :-

output = new JButton ("Convert to Celsius: ");

and then overwriting it a few lines below here:-

output = new JButton ("Convert to Fahrenheit: ");
results_c = new JLabel (" ");

So sort out which initialization do you need.

i still cant get it working,
im not sure what u mean

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.