Borderless Frame pack()


Borderless Frame pack()

Summary: By the end of this tutorial "Borderless Frame pack()", you will understand how to create a borderless frame and pack() method.

1. Creating a borderless window

Frame is known as top-level window as it contains border and title bar (panel is not a top-level window; it is always a child window and does not have existence without a top-level window). A frame can be created without a border but of little practical use.

import java.awt.*;
public class NoBorder                     // no Frame extending
{
  public NoBorder()
  {
    Frame fr = new Frame();
    Window win = new Window(fr);
    win.setLayout(new FlowLayout());
    Button btn= new Button("OK");
    win.add(btn);
    win.setBounds(60,70, 250, 250);

    win.setSize(300, 300);
    win.setVisible(true);
  }
  public static void main(String args[])
  {
    new NoBorder();
  }
}


Borderless Frame pack()

Output screen of NoBorder.java of Borderless Frame pack()

Window is the super class of Frame (see the container hierarchy). Frame is added to window. Button is added to window and window is added to monitor. Window gives a generic window. Observe, the class does not extend Frame.

2. Packing the Components with pack()

If you would like to have the frame in it's preferred size (with all embedded components), just give pack() instead of setSize() method. The following program explains.

import java.awt.*; 
public class PackDemo extends Frame  
{
 public PackDemo()  
 {
   setLayout(new FlowLayout()); 
                                          
   add(new Button("OK"));                                                                                                                                                     
                                                            
   setTitle("Pack Style"); 
   pack();                                  // pack() instead of setSize()
   setVisible(true);            
 }
 public static void main( String args[])  
 {  
   new PackDemo();   
 }
}

Borderless Frame pack()
pack() method displays a frame just sufficient to display the components and the three icons (minimize, maximize, close) on the title bar. The frame can be resized by dragging on the borders. This style without border is not used in regular practice.

===================Extra Reading=============================

To add more features to AWT components: Advanced AWT (Swing Components)

Leave a Comment

Your email address will not be published.