Help with radio buttons and results.
I need help completing this Ideal weight calculator where the formula is
Use the approximate formula:
W = H2 / 30 , for female
W = H2 / 28 , for male
where W is the ideal weight in pounds, H is the height in inches. The ideal weight will display in the JLabel text box. I can not figure out where to input the action listeners to correspond with the formula. The code compiles but the buttons do not display the ideal weight.
My code so far is:
Code Java:
import java.awt.* ;
import java.awt.event.*;
import javax.swing.*;
public class Assignment4_Cheshire extends JFrame
{
JRadioButton genderM, genderF;
ButtonGroup genderGroup;
JPanel genderPanel;
JRadioButton heightA, heightB, heightC, heightD, heightE;
ButtonGroup heightGroup;
JPanel heightPanel;
JTextField resultText;
JLabel resultLabl;
JPanel resultPanel;
public Assignment4_Cheshire()
{
setTitle( "Your Ideal Weight" );
setDefaultCloseOperation( EXIT_ON_CLOSE );
// gender group
genderM = new JRadioButton("Male", true );
genderF = new JRadioButton("Female", false );
genderGroup = new ButtonGroup();
genderGroup.add( genderM );
genderGroup.add( genderF );
genderPanel = new JPanel();
genderPanel.setLayout( new BoxLayout( genderPanel, BoxLayout.Y_AXIS ) );
genderPanel.add( new JLabel("Your Gender") );
genderPanel.add( genderM ); genderPanel.add( genderF );
// height group
heightA = new JRadioButton("60 to 64 inches", true );
heightB = new JRadioButton("64 to 68 inches", false );
heightC = new JRadioButton("68 to 72 inches", false );
heightD = new JRadioButton("72 to 76 inches", false );
heightE = new JRadioButton("76 to 80 inches", false );
heightGroup = new ButtonGroup();
heightGroup.add( heightA ); heightGroup.add( heightB );
heightGroup.add( heightC ); heightGroup.add( heightD );
heightGroup.add( heightE );
heightPanel = new JPanel();
heightPanel.setLayout( new BoxLayout( heightPanel, BoxLayout.Y_AXIS ) );
heightPanel.add( new JLabel("Your Height") );
heightPanel.add( heightA ); heightPanel.add( heightB );
heightPanel.add( heightC ); heightPanel.add( heightD );
heightPanel.add( heightE );
// result panel
resultText = new JTextField(7);
resultText.setEditable( false );
resultLabl = new JLabel("Ideal Weight");
resultPanel = new JPanel();
resultPanel.add( resultLabl );
resultPanel.add( resultText );
// frame
setLayout( new BorderLayout() );
add( genderPanel, BorderLayout.WEST );
add( heightPanel, BorderLayout.EAST );
add( resultPanel, BorderLayout.SOUTH );
}
public static void main ( String[] args )
{
Assignment4_Cheshire weightApp = new Assignment4_Cheshire() ;
weightApp.setSize( 250, 225 );
weightApp.setResizable( false );
weightApp.setVisible( true );
}
}
Any guidance will be appreciacted.
thanks
Samantha
Re: Help with radio buttons and results.
There may be a better way but you could always use the boolean method isSelected() for the RadioButtons.
Also, how is your formula supposed to work for a range?
Code java:
if (genderM.isSelected())
{
if (heightA.isSelected())
{
Double temp = Math.pow(60,2) /28;
resultText.setText(temp.toString());
}
Re: Help with radio buttons and results.
Thank you for the reply.
The height range is used to calculate the weight range for either male or female.
I have tried to get the radio buttons to work and I am still stuck. not sure where to add the calculations in.
Code Java:
import java.awt.* ;
import java.awt.event.*;
import javax.swing.*;
public class Assignment4_Cheshire extends JFrame
{
JRadioButton genderM, genderF;
ButtonGroup genderGroup;
JPanel genderPanel;
JRadioButton heightA, heightB, heightC, heightD, heightE;
ButtonGroup heightGroup;
JPanel heightPanel;
JTextField resultText;
JLabel resultLabl;
JPanel resultPanel;
public Assignment4_Cheshire()
{
setTitle( "Your Ideal Weight" );
setDefaultCloseOperation( EXIT_ON_CLOSE );
// gender group
genderM = new JRadioButton("Male", true );
genderF = new JRadioButton("Female", false );
genderGroup = new ButtonGroup();
genderGroup.add( genderM );
genderGroup.add( genderF );
genderPanel = new JPanel();
genderPanel.setLayout( new BoxLayout( genderPanel, BoxLayout.Y_AXIS ) );
genderPanel.add( new JLabel("Your Gender") );
genderPanel.add( genderM ); genderPanel.add( genderF );
// height group
heightA = new JRadioButton("60 to 64 inches", true );
heightB = new JRadioButton("64 to 68 inches", false );
heightC = new JRadioButton("68 to 72 inches", false );
heightD = new JRadioButton("72 to 76 inches", false );
heightE = new JRadioButton("76 to 80 inches", false );
heightGroup = new ButtonGroup();
heightGroup.add( heightA ); heightGroup.add( heightB );
heightGroup.add( heightC ); heightGroup.add( heightD );
heightGroup.add( heightE );
heightPanel = new JPanel();
heightPanel.setLayout( new BoxLayout( heightPanel, BoxLayout.Y_AXIS ) );
heightPanel.add( new JLabel("Your Height") );
heightPanel.add( heightA ); heightPanel.add( heightB );
heightPanel.add( heightC ); heightPanel.add( heightD );
heightPanel.add( heightE );
// result panel
resultText = new JTextField(7);
resultText.setEditable( false );
resultLabl = new JLabel("Ideal Weight");
resultPanel = new JPanel();
resultPanel.add( resultLabl );
resultPanel.add( resultText );
// frame
setLayout( new BorderLayout() );
add( genderPanel, BorderLayout.WEST );
add( heightPanel, BorderLayout.EAST );
add( resultPanel, BorderLayout.SOUTH );
}
public static void main ( String[] args )
{
Assignment4_Cheshire weightApp = new Assignment4_Cheshire() ;
weightApp.setSize( 250, 225 );
weightApp.setResizable( false );
weightApp.setVisible( true );
}
}
Re: Help with radio buttons and results.
Hello Bewitched1,
Sorry I have only just found this thread. Did you solve this issue in the end?