?
Solved

Java GUI

Posted on 2011-10-08
5
Medium Priority
?
1,066 Views
Last Modified: 2012-05-12
I need to enhance this program by adding the Kelvin scale. The program should all the user to make conversions between two scales by using the following formula Kelvin=Celsius+273.13. I have the program that needs the enhancement. The program after that is my attempt at enhancing the 14.12 program. Can you help me finish 14.13 program.

import javax.swing.JOptionPane;
import javax.swing.JLabel;
import javax.swing.JFrame;

public class LabelFrame extends JFrame
{

private JLabel label; // converted temperature

public LabelFrame()
{
super( "Temperature Conversions" );

// obtain user input from JOptionPane input dialogs
String first =
JOptionPane.showInputDialog( "Enter Fahrenheit Temperature" );

// convert String input to int values for use in calculation
double num = Double.parseDouble( first );
double celsius = ( 5.0/9.0 * ( num - 32 ));


// displays temperature conversion
JLabel label = new JLabel( "The Celsius Temperture is " +celsius );
add( label );

} // end method LabelFrame
} // end class LabelFrame


import javax.swing.JOptionPane;
import javax.swing.JLabel;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;

public class LabelFrame extends JFrame
{

private JLabel label; // converted temperature
private JRadioButton rbCelsius;
private JRadioButton rbKelvin;
private ButtonGroup radioGroup;
private JButton button;


public LabelFrame()
{
super( "Temperature Conversions" );

// obtain user input from JOptionPane input dialogs
String first =
JOptionPane.showInputDialog( "Enter Fahrenheit or Celsius Temperature" );

rbCelsius= new JRadioButton ( "Celsius", true );
add( rbCelsius );

rbKelvin = new JRadioButton( "Kelvin", false );
add( rbKelvin );

radioGroup = new ButtonGroup();
radioGroup.add( rbCelsius );
radioGroup.add( rbKelvin );

// convert String input to int values for use in calculation
double num = Double.parseDouble( first );

double kelvin = celsius + 273.15;

JLabel label1 = new JLabel( "The Kelvin Temperature is " +kelvin );
add( label1 );

double celsius = ( 5.0/9.0 * ( num - 32 ));

JLabel label2 = new JLabel( "The Celsius Temperture is " +celsius );
add( label2 );

button = new JButton( "Results" );
add( button );
button.addActionListener( this );

results = new JLabel ("Enter Ferenheit, Choose an option to convert and Click Show Result");
add( results );
}

public void actionPerformed( ActionEvent e )
{
double fahrenheit = Double.parseDouble( first ));

double celsius = 0.0;

celsius = ( (5.0/9.0) * ( fahrenheit - 32.0 ) );

if ( rbKelvin.isSelected() )
celsius += 273.15;

results =
}


} // end method LabelFrame
} // end class LabelFrame


import javax.swing.JFrame;

public class LabelTest
{
public static void main( String args[] )
{
LabelFrame labelFrame = new LabelFrame(); // creates TemperatureConversion
labelFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
labelFrame.setSize( 300, 300 ); // set frame size
labelFrame.setVisible( true ); // display frame

} // end method main
} // end class TemperatureConversionTest


0
Comment
Question by:assaultkitty
  • 4
5 Comments
 
LVL 47

Accepted Solution

by:
for_yan earned 2000 total points
ID: 36937368



You just need to modify your first class by
repacing the formula with this:

double celsius = num - 273.13;


import javax.swing.JOptionPane;
import javax.swing.JLabel;
import javax.swing.JFrame;

public class LabelFrame extends JFrame
{

private JLabel label; // converted temperature

public LabelFrame()
{
super( "Temperature Conversions" );

// obtain user input from JOptionPane input dialogs
String first =
JOptionPane.showInputDialog( "Enter Kelvin Temperature" );

// convert String input to int values for use in calculation
double num = Double.parseDouble( first );
double celsius = num - 273.13;


// displays temperature conversion
JLabel label = new JLabel( "The Celsius Temperture is " +celsius );
add( label );

} // end method LabelFrame
} // end class LabelFrame

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36937370
Oh, sorry you need to add one more conversion. Just a moment
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36937377

this would show both.
You need to add these:

Container c = this.getContentPane();
    c.setLayout(new FlowLayout());

and then add your labels to Container
to make sure bothe of the labels are visible



import javax.swing.JOptionPane;
import javax.swing.JLabel;
import javax.swing.JFrame;
import java.awt.*;

public class LabelFrame extends JFrame
{

private JLabel label; // converted temperature

public LabelFrame()
{
super( "Temperature Conversions" );

// obtain user input from JOptionPane input dialogs
String first =
JOptionPane.showInputDialog( "Enter Fahrenheit Temperature" );

// convert String input to int values for use in calculation
double num = Double.parseDouble( first );
double celsius = ( 5.0/9.0 * ( num - 32 ));

double kelvin = celsius + 273.13;

Container c = this.getContentPane();
    c.setLayout(new FlowLayout());

// displays temperature conversion
JLabel label = new JLabel( "The Celsius Temperture is " +celsius );
c.add( label );

JLabel label1 = new JLabel( "The Kelvin  Temperture is " +kelvin );
c.add( label1 );    


} // end method LabelFrame
} // end class LabelFrame

Open in new window

0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 2000 total points
ID: 36937384
It would be a good idea to add a little bit of number formattiing
so that it does not print too many digits after
decimal point:

import javax.swing.JOptionPane;
import javax.swing.JLabel;
import javax.swing.JFrame;
import java.awt.*;
import java.text.DecimalFormat;
import java.text.NumberFormat;

public class LabelFrame extends JFrame
{

private JLabel label; // converted temperature

public LabelFrame()
{
super( "Temperature Conversions" );
    NumberFormat ff = new DecimalFormat("#####.##");

// obtain user input from JOptionPane input dialogs
String first =
JOptionPane.showInputDialog( "Enter Fahrenheit Temperature" );

// convert String input to int values for use in calculation
double num = Double.parseDouble( first );
double celsius = ( 5.0/9.0 * ( num - 32 ));

double kelvin = celsius + 273.13;

Container c = this.getContentPane();
    c.setLayout(new FlowLayout());

// displays temperature conversion
JLabel label = new JLabel( "The Celsius Temperture is   " +ff.format(celsius) );
c.add( label );

JLabel label1 = new JLabel( "The Kelvin  Temperture is   " +ff.format(kelvin) );
c.add( label1 );    


} // end method LabelFrame
} // end class LabelFrame

Open in new window

0
 

Author Closing Comment

by:assaultkitty
ID: 36938585
You are a lifesaver! Thanks.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month14 days, 15 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question