Problem with radio button loop in temperature conversion program.

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

32 Replies - 1443 Views - Last Post: 07 August 2013 - 09:58 AM Rate Topic: -----

#31 Dubyer  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 13-July 13

Re: Problem with radio button loop in temperature conversion program.

Posted 07 August 2013 - 09:03 AM

The user just has to input a value, and click on whichever conversion they want to do, hit enter and it should display in the output. But since I can't get all 6 of my radio buttons into the frame, it only shows 1 set of them.

This post has been edited by Dubyer: 07 August 2013 - 09:07 AM

Was This Post Helpful? 0
  • +
  • -

#32 Dubyer  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 13-July 13

Re: Problem with radio button loop in temperature conversion program.

Posted 07 August 2013 - 09:50 AM

I am finally finished, thank you pbl, after looking at your code, I integrated that into my program, and I got the output that I wanted.

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

public class ConvertFrame extends JFrame {
private JPanel convertFrom = new JPanel(); 
private JPanel convertTo = new JPanel();
private JLabel label1 = new JLabel("Input");
private JLabel label2 = new JLabel("Output");
private JLabel label3 = new JLabel("Input Scale"); 
private JLabel label4 = new JLabel("Output Scale");
private JTextField temperature1 = new JTextField(7);
private JTextField temperature2 = new JTextField(7);
private ButtonGroup radioFrom = new ButtonGroup(); 
private ButtonGroup radioTo = new ButtonGroup();
private JRadioButton celciusBoxTo = new JRadioButton("Celcius");
private JRadioButton fahrenheitBoxTo = new JRadioButton("Fahrenheit");
private JRadioButton kelvinBoxTo = new JRadioButton("Kelvin");
private JRadioButton celciusBoxFrom = new JRadioButton("Celcius");
private JRadioButton fahrenheitBoxFrom = new JRadioButton("Fahrenheit");
private JRadioButton kelvinBoxFrom = new JRadioButton("Kelvin");

// set up GUI
public ConvertFrame()
{
super("Converter");

fahrenheitBoxFrom = new JRadioButton("Fahrenheit", false);
celciusBoxFrom = new JRadioButton("Celcius", true);
kelvinBoxFrom = new JRadioButton("Kelvin", false);
JPanel label3 = new JPanel(new GridLayout(6,1));
label3.add(new JLabel("Input Scale"), BorderLayout.WEST);
add(label3, BorderLayout.WEST);
JPanel radioFrom = new JPanel(new GridLayout(3, 1));
label3.add(celciusBoxFrom);
label3.add(fahrenheitBoxFrom);
label3.add(kelvinBoxFrom);
add(radioFrom);

fahrenheitBoxTo = new JRadioButton("Fahrenheit", true);
celciusBoxTo = new JRadioButton("Celcius", false);
kelvinBoxTo = new JRadioButton("Kelvin", false);
JPanel label4 = new JPanel(new GridLayout(6,1));
label4.add(new JLabel("Output Scale"), BorderLayout.EAST);
add(label4, BorderLayout.EAST);
JPanel radioTo = new JPanel(new GridLayout(3,1));
label4.add(celciusBoxTo);
label4.add(fahrenheitBoxTo);
label4.add(kelvinBoxTo);
add(radioTo);


JPanel label1 = new JPanel();
label1.setLayout(new FlowLayout());
label1.add(new JLabel("Input"), BorderLayout.WEST);
label1.add(temperature1, BorderLayout.CENTER);
temperature1.setHorizontalAlignment(JTextField.CENTER);
add(label1, BorderLayout.NORTH);


JPanel label2 = new JPanel();
label2.setLayout(new FlowLayout());
label2.add(new JLabel("Output"), BorderLayout.WEST);
label2.add(temperature2, BorderLayout.CENTER);
temperature2.setHorizontalAlignment(JTextField.CENTER);
temperature2.setEditable(false);
add(label2, BorderLayout.SOUTH);


ButtonGroup convertFrom = new ButtonGroup();
convertFrom.add(celciusBoxFrom);
convertFrom.add(fahrenheitBoxFrom);
convertFrom.add(kelvinBoxFrom);
 
	 
 
ButtonGroup convertTo = new ButtonGroup();
convertTo.add(celciusBoxTo);
convertTo.add(fahrenheitBoxTo);
convertTo.add(kelvinBoxTo);


setSize( 300, 200 );
setVisible( true );

temperature1.addActionListener(

new ActionListener() { // anonymous inner class

// perform conversions
public void actionPerformed( ActionEvent event )
{
double convertTemp, temp;
DecimalFormat df = new DecimalFormat("0.00");


temp = Integer.parseInt( ( ( JTextField )
event.getSource() ).getText() );

// fahrenheit to celcius
if ( fahrenheitBoxFrom.isSelected() &&
celciusBoxTo.isSelected()) 
{
convertTemp = (double) ((5.0/9.0)*(temp-32));
temperature2.setText(String.valueOf(convertTemp));
temperature2.setText(" "+ (df.format(convertTemp)) +" C");
}
// fahrenheit to kelvin
else if ( fahrenheitBoxFrom.isSelected() &&
kelvinBoxTo.isSelected()) 
{
convertTemp = (double)((5.0/9.0)*(temp-32)+273);
temperature2.setText(String.valueOf(convertTemp));
temperature2.setText(" "+ (df.format(convertTemp)) +" K");
}

// celcius to fahrenheit
else if ( celciusBoxFrom.isSelected() &&
fahrenheitBoxTo.isSelected()) 
{
convertTemp = (double) ((9.0/5.0)*temp+32);
temperature2.setText(String.valueOf(convertTemp));
temperature2.setText(" "+ (df.format(convertTemp))+" F");
}

// celcius to kelvin
else if ( celciusBoxFrom.isSelected() &&
kelvinBoxTo.isSelected()) 
{
convertTemp = (temp+273);
temperature2.setText(String.valueOf(convertTemp));
temperature2.setText(" "+ (df.format(convertTemp)) + " K");
}

// kelvin to celcius
else if ( kelvinBoxFrom.isSelected() &&
celciusBoxTo.isSelected())
{
convertTemp = (temp-273);
temperature2.setText(String.valueOf(convertTemp));
temperature2.setText(" "+ (df.format(convertTemp))+ " C");
}

// kelvin to fahrenheit
else if ( kelvinBoxFrom.isSelected() &&
fahrenheitBoxTo.isSelected()) 
{
convertTemp = (double) ((5.0/9.0)*(temp-273)+32);
temperature2.setText(String.valueOf(convertTemp));
temperature2.setText(" "+ (df.format(convertTemp)) + " F");
}

} // end method actionPerformed

} // end anonymous inner class

); // end call to addActionListener


}} // end constructor




Attached Image What my lab wanted the output to look like.
Attached Image What I have, finally looks the same!

Thank you all everyone who has helped me along the way.

This post has been edited by Dubyer: 07 August 2013 - 09:52 AM

Was This Post Helpful? 0
  • +
  • -

#33 BlueMelon  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 187
  • Joined: 27-April 10

Re: Problem with radio button loop in temperature conversion program.

Posted 07 August 2013 - 09:58 AM

If the textbox text wasn't centered and you added some padding to your radio buttons it would look even more similar.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3