go to previous page   go to home page   go to next page

Answer:

Two


Gender Buttons

Your Ideal Weight

Here is the part of the program for the gender buttons. The buttons are added to the genderPanel, which controls their graphic representation on screen, and added to the genderGroup, which controls their logical operation.

BoxLayout is used to put the label and the two radio buttons in a vertical column. The JLabel is constructed and added to the panel in a single statement. This is fine, since there will be no need to refer to the label as a variable.

public class IdealWeight extends JFrame
{
  
  JRadioButton genderM;
  JRadioButton genderF;
  ButtonGroup  genderGroup;
  JPanel       genderPanel;
  . . . .
  public IdealWeight()  
  { 
    // 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.add( new JLabel("Your Gender") );
    genderPanel.add( genderM );
    genderPanel.add( genderF );
   . . . . .

QUESTION 4:

Decide on the parameters for BoxLayout and fill in the blanks.

(Remember that the constructor needs a reference to the component that will use the new layout manager.)