Adding Panels to the Frame

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:

  JPanel fatPanel   = new JPanel();
  JPanel calPanel   = new JPanel();
  JPanel perPanel   = new JPanel();
  
  public percentFatPanel()   
  {  
    setTitle( "Calories from Fat" );
    outPer.setEditable( false );   
    setLayout( new FlowLayout() ); 

    hedPanel.add( heading );
    fatPanel.add( fatLabel );
    fatPanel.add( inFat );
    calPanel.add( calLabel );
    calPanel.add( inCal );
    perPanel.add( perLabel );
    perPanel.add( outPer );
    butPanel.add( doit );
    . . . .
  }

Adding Panels to the Frame

Now add the panels to the frame. This is being done in the constructor for the JFrame so add() means use the method of that frame.


  . . . . .
   
  JPanel hedPanel   = new JPanel();
  JPanel fatPanel   = new JPanel();
  JPanel calPanel   = new JPanel();
  JPanel perPanel   = new JPanel();   
  JPanel butPanel   = new JPanel();   
  
  public percentFatPanel()   
  { 
    setTitle( "Calories from Fat" );
    outPer.setEditable( false );   
    setLayout( new FlowLayout() ); 
 
    hedPanel.add( heading );
    fatPanel.add( fatLabel );
    fatPanel.add( inFat );
    calPanel.add( calLabel );
    calPanel.add( inCal );
    perPanel.add( perLabel );
    perPanel.add( outPer );
    butPanel.add( doit );
    
    add(  );
    add(  );
    add(  );
    add(  );
    add(  );
 
    . . . . . .
  }

QUESTION 6:

Add the components from top to bottom