multiple button groups
Hey everyone I am writing a program that contains 2 button groups(of radio buttons) and I am try to set a different value of Height for every height, and either the gender of male or female. But i can't get the getActionCommand to work with it. I don't know how to make it select one button for each group here is my code.
import java.awt.* ;
import java.awt.event.*;
import javax.swing.*;
publicclass IdealWeightCalculatorextends JFrameimplements ActionListener
{
JRadioButton genderM, genderF;
ButtonGroup genderGroup;
JPanelgenderPanel;
JRadioButton heightA, heightB, heightC, heightD, heightE;
ButtonGroup heightGroup;
JPanelheightPanel;
JTextFieldresultText;
JLabelresultLabl;
JPanelresultPanel;
double W,H;
public IdealWeightCalculator()
{
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 );
// content pane
getContentPane().setLayout(new BorderLayout() );
getContentPane().add( genderPanel, BorderLayout.WEST );
getContentPane().add( heightPanel, BorderLayout.EAST );
getContentPane().add( resultPanel, BorderLayout.SOUTH );
genderM.setActionCommand("male");
genderF.setActionCommand("female");
heightA.setActionCommand("1");
heightB.setActionCommand("2");
heightC.setActionCommand("3");
heightD.setActionCommand("4");
heightE.setActionCommand("5");
genderM.addActionListener(this );
genderF.addActionListener(this );
heightA.addActionListener(this );
heightB.addActionListener(this );
heightC.addActionListener(this );
heightD.addActionListener(this );
heightE.addActionListener(this );
}
publicvoid malecalc(){
W=(H*H)/28;
}
publicvoid femalecalc(){
W=(H*H)/30;
}
publicvoid actionPerformed( ActionEvent evt)
{
/*if (evt.getActionCommand().equals("male")){
if (evt.getActionCommand().equals("1")){
H=62;
malecalc();
}
}*/
if (evt.getActionCommand().equals("male")){
if(evt.getActionCommand().equals("1")){
H=62;
malecalc();
}
elseif (evt.getActionCommand().equals("2")){
H=66;
malecalc();
}
elseif (evt.getActionCommand().equals("3")){
H=70;
malecalc();
}
elseif (evt.getActionCommand().equals("4")){
H=74;
malecalc();
}
elseif (evt.getActionCommand().equals("5")){
H=78;
malecalc();
}
}
elseif (evt.getActionCommand().equals("female")){
if(evt.getActionCommand().equals("1")){
H=62;
femalecalc();
}
elseif (evt.getActionCommand().equals("2")){
H=66;
femalecalc();
}
elseif (evt.getActionCommand().equals("3")){
H=70;
femalecalc();
}
elseif (evt.getActionCommand().equals("4")){
H=74;
femalecalc();
}
elseif (evt.getActionCommand().equals("5")){
H=78;
femalecalc();
}
}
resultText.setText( (W+"") );
repaint();
}
publicstaticvoid main ( String[] args )
{
IdealWeightCalculator weightApp =new IdealWeightCalculator() ;
weightApp.setSize( 250, 225 );
weightApp.setVisible(true );
}
}

