Gender Buttons

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
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 (1) genderPanel, which controls their graphic representation on screen, and added to (2) 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.