• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1090
  • Last Modified:

Java GUI

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
assaultkitty
Asked:
assaultkitty
  • 4
2 Solutions
 
for_yanCommented:



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
 
for_yanCommented:
Oh, sorry you need to add one more conversion. Just a moment
0
 
for_yanCommented:

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
 
for_yanCommented:
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
 
assaultkittyAuthor Commented:
You are a lifesaver! Thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now