Complete User Interface

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:

// Add Panels to the Frame
setLayout( new BorderLayout() );
add( genderPanel, BorderLayout.WEST );
add( heightPanel, BorderLayout.EAST );
add( resultPanel, BorderLayout.SOUTH );

Complete User Interface

The unused sections (NORTH and CENTER) shrink to make room for the sections that do hold components. Here is the complete GUI part of the program. (The application code that calculates the ideal weight has been left for the exercises.)

The setLayout( new BorderLayout() ) is not really needed because BorderLayout is the default layout manager for class JFrame

import java.awt.* ;
import java.awt.event.*;
import javax.swing.*;
  
public class IdealWeight 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 IdealWeight()  
  { 
    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 )
  {
    IdealWeight weightApp  = new IdealWeight() ;
    weightApp.setSize( 250, 225 );  
    weightApp.setResizable( false ); 
    weightApp.setVisible( true );        
  }
}

QUESTION 8:

Could you create a similar layout using BoxLayout for the frame?