this program is 2 classes and it has 2 JLabels, 2 JTextFileds, and 2 JButtons...
it convert temperatures from Fahrenheit to Celsius and from Celsius to Fahrenheit..

Class 1:

// Author: Helen Toma

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.JTextField;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;

public class ConvertTemp extends JFrame {
	// instance variables 
	JLabel labelFahr;
	JLabel labelCels;
	JTextField textFahr;
	JTextField textCels;
	JButton conFahToCel;
	JButton conCelToFah;
	
	public ConvertTemp () {
		
		super ("Temerature");
		setLayout (new FlowLayout ());
		
		labelFahr = new JLabel ("Fahrenheit: ", SwingConstants.LEFT);
		labelFahr.setToolTipText("This is a temerature scale");
		add (labelFahr);
		textFahr = new JTextField (10);
		add (textFahr);
		
		labelCels = new JLabel ("Celsius:       ", SwingConstants.LEFT);
		labelCels.setToolTipText("This is a scale and unit of measurement for temperature");
		add (labelCels);
		textCels = new JTextField (10);
		add (textCels);
		
		conFahToCel = new JButton ("Convert Fahrenheit to Celsius");
		add (conFahToCel);
		conCelToFah = new JButton ("Convert Celsius to Fahrenheit");
		add (conCelToFah);
		
		JPanel panel = new JPanel(new GridLayout(2, 2, 12, 6));
		panel.add(labelFahr);
		panel.add(labelCels);
		panel.add(textFahr);
		panel.add(textCels);
		add(panel, BorderLayout.NORTH);
		
		JPanel buttonPanel = new JPanel();
		buttonPanel.add(conFahToCel);
		buttonPanel.add(conCelToFah);	
		add(buttonPanel, BorderLayout.SOUTH);
		
		conFahToCel.addActionListener(new FahrListener ());
		conCelToFah.addActionListener(new CelsListener ());
		
	} // end constructor 
	
	private class FahrListener implements ActionListener {
		public void actionPerformed(ActionEvent event) {
			if (event.getSource() == conFahToCel){
				int conFahToCel = (int) ((5.0/9.0 * (((Double.parseDouble(textFahr.getText())) -32))));
				textCels.setText(conFahToCel + " °C");
				textFahr.requestFocus();
				textFahr.selectAll();
			} // end if statement
			
		} // end actionPerformed
	} // end FahrListener
	
	private class CelsListener implements ActionListener {
		public void actionPerformed(ActionEvent event) {
			if (event.getSource() == conCelToFah){
				int conCelToFah = (int) (( 9.0/5.0 * (((Double.parseDouble(textCels.getText())) + 32))));
				textFahr.setText(conCelToFah + " °F");
				textCels.requestFocus();
				textCels.selectAll();
			} // end if statement
			
		} // end actionPerformed
	} // end CelsListener
	
} // end class ConvertTemp

---------------------------------------------------------------------------------------

Class 2:

// Author: Helen Toma

import javax.swing.JFrame;

public class ConvertTempTest {

	public static void main(String[] args) {
		
		ConvertTemp app = new ConvertTemp ();
		app.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
		app.setSize (500, 150); // set the frame size
		app.setVisible (true); // display frame

	} // end main method

} // end class ConvertTempTest

Do you have any questions or problems about the posted code?

Please edit your post and wrap the code in code tags. Use the [code] icon above the input box.

textCels.setText(conFahToCel + " °C");
..
..
textFahr.setText(conCelToFah + " °F");

Hint: Make sure to strip all non-numeric characters before converting strings to double.

Is there any problem with the code? or Is it working perfectly?

Can ask you a few question
How to do the convert units ?
I need to create a GUI applet to convert units
The required to provide conversion for at least 8 categories each with a minimum of 2 conversion items for each category.

Please start a new thread for your questions. This one is 11 months old.

How to code the GUI applet convert units ?
I want to code using netbeans code Fahrenheit to Celcius .
And how to shows the result ? Can you please help me .

Please start a new thread for your questions. This one is 11 months old.

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.