// Add Panels to the Frame setLayout( new BorderLayout() ); add( genderPanel, BorderLayout.WEST ); add( heightPanel, BorderLayout.EAST ); add( resultPanel, BorderLayout.SOUTH );
The unused sections (NORTH and CENTER) shrink to make room for the sections that do hold components. Here is the complete GUI for the program. The application code that calculates the ideal weight has been left for the exercises.
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: use default layout manager 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 ); } }
Could you create a similar layout using BoxLayout
for the frame?