PackDemo.java

Download PackDemo.java

// Demo of using Container.pack() method.
//
// Written 4/2006 by Wayne Pollock, Tampa Florida USA.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class PackDemo
{
    public static void main ( String [] args )
    {
       final JFrame frame = new JFrame( "Pack Demo" );
       frame.setSize( 500, 350 );
       frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
//       frame.setLayout( new FlowLayout() );

       JButton btn = new JButton( "Pack Frame" );
       frame.add( btn, BorderLayout.SOUTH );
       btn.addActionListener( new ActionListener() {
         public void actionPerformed ( ActionEvent e )
         {
            frame.pack();
         }
       } );

       JLabel lbl = new JLabel( "A JLabel" );
       lbl.setBackground( Color.CYAN );
       frame.add( lbl, BorderLayout.EAST );

       frame.add( new JButton( "Click Here" ), BorderLayout.CENTER );

       frame.setVisible( true );
    }
}