0

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

Edited by peter_budo: Keep It Clear - Do use code tags when posting any code

7
Contributors
8
Replies
19
Views
5 Years
Discussion Span
Last Post by JamesCherrill
Featured Replies
  • 1

    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. Read More

1

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.

Edited by NormR1: n/a

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

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

0

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.

0

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 .

This article has been dead for over six months. 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.