Answer:

See Below.

Complete Program

Here is the new version of the percent fat program. You may copy it to an editor, save it, and run it. When you run it, resize the frame and observe the effect. The panels remain aligned in a column. Making the frame larger merely increases the distance between panels.

import java.awt.* ;
import java.awt.event.*;
import javax.swing.*;
   
public class percentFatPanel extends JFrame implements ActionListener
{
  JLabel title      = new JLabel("Percent of Calories from Fat");
  JLabel fatLabel   = new JLabel("Enter grams of fat:   ");
  JLabel calLabel   = new JLabel("Enter total calories: ");
  JLabel perLabel   = new JLabel("Percent calories from fat: ");
    
  JTextField inFat  = new JTextField( 7 );
  JTextField inCal  = new JTextField( 7 );
  JTextField outPer = new JTextField( 7 );
    
  JButton    doit   = new JButton("Do It!");
   
  JPanel fatPanel   = new JPanel();
  JPanel calPanel   = new JPanel();
  JPanel perPanel   = new JPanel();

  int calories ;  // input: total calories per serving
  int fatGrams ;  // input: grams of fat per serving
  double percent; // result: percent of calories from fat
   
  public percentFatPanel()   
  {  
    setTitle( "Calories from Fat" );
    outPer.setEditable( false );    
    getContentPane().setLayout(
        new BoxLayout( getContentPane(), BoxLayout.Y_AXIS ) ); 
    
    fatPanel.add( fatLabel );
    fatPanel.add( inFat );
    calPanel.add( calLabel );
    calPanel.add( inCal );
    perPanel.add( perLabel );
    perPanel.add( outPer );
      
    getContentPane().add( title );
    getContentPane().add( fatPanel );
    getContentPane().add( calPanel );
    getContentPane().add( perPanel );
    getContentPane().add( doit );
  
    doit.addActionListener( this );
    setDefaultCloseOperation( WindowConstants.EXIT_ON_CLOSE );   
  }
   
  // The application
  public void calcPercent( )  
  {
    percent = ( (fatGrams * 9.0) / calories ) * 100.0 ;
  }
   
  public void actionPerformed( ActionEvent evt)  
  {
    String userIn ;
    userIn    = inFat.getText()  ;
    fatGrams  = Integer.parseInt( userIn ) ;
   
    userIn    = inCal.getText()  ;
    calories  = Integer.parseInt( userIn ) ;
   
    calcPercent() ;
   
    outPer.setText( (percent+"      ").substring(0,6)  );
    repaint();                  
  }
   
  public static void main ( String[] args )
  {
    percentFatPanel fatApp  = new percentFatPanel() ;
    fatApp.setSize( 300, 225 );     
    fatApp.setVisible( true );         
  }
}

QUESTION 8:

(Thought Question: ) Could a JPanel be placed inside another JPanel?