Frame and Panel Containers -


Frame -

Frame class is used to create top level or child windows. In contrast with Applet, Frame windows have border and title, whereas Applet does not have.
The default layout of Frame is border layout, whereas Applet have flow layout. Layouts will be covered next.
The most commonly used constructors to create Frame are as follows -
1- public Frame() throws HeadlessException - 
Creates a Frame with no title specified.
2- public Frame(String title) throws HeadlessException - 
Creates a Frame with specified title.

Note that Frame is invisible by default and does not have any specified dimensions. So we can define dimensions of Frame after creating Frame object as follows -

Specifying Frame Dimensions -

java.awt.Window class provides setSize() method to define frame dimensions. It has two versions -
1- public void setSize(int width, int height)
Set the size of  the Frame to specified width and height.
2- public void setSize(Dimension dim)
Set the size of the frame to the specified Dimension object.
Here Dimension class object specify width and height. 
The most commonly used constructor of Dimension class is -
  • public Dimension(int width, int height)
    Where width and height specify the width and height of component.
    
  • We can also get the size of Frame in terms of Dimension object -
    public Dimension getSize()
    It returns the Dimension class object that contain Frame width and height.
    

Example -

Frame fr = new Frame();  
// create an empty frame with no title and dimesions
Now there are two methods to specify the dimensions of frame -
1- fr.setSize(400, 400);  
// set the size of frame (frame width = 400 pixels and frame height = 400 pixels)
Or we can also use Dimension object as follows -
2- Dimension dim = new Dimension(400, 400); 
// set component size (component width = 400 pixels and height = 400 pixels)
fr.setSize(dim);

Making Frame window visible -

Frame is invisible by default. We can make frame visible by using setVisible() method -
public void setVisible(boolean val)
If the val is True, then Frame will be visible otherwise frame will be invisible.

For Example -

fr.setVisible(true);

Setting and getting Frame Window Title -

We can assign or change the title of a frame by using setTitle() method. It has the following form -
public void  setTitle(String title)
Where title is the new title of the frame.
We can also get the frame title by using getTitle() method. It has the following general form -
public String getTitle()

For Example -

fr.setTitle("My New Frame");
String titile = fr.getTitle();  // return the current title of frame

Closing Frame Window -

When using frame window, your program must remove that window from the screen when it is closed. We can do it by using windowClosing() method of WindowListener interface.

For Example -

fr.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e)
            {
                System.exit(0);
            }

        });
    

Example:- Creating Frame -

This example will create a frame with title "My Frame" and width and height of frame will be 400,400 pixels respectively.
import java.awt.*;
import java.awt.event.*;
public class Test 
{
    public static void main(String args[])
    {
        Frame fr = new Frame("My Frame");
        fr.setVisible(true);
        fr.setSize(400, 400);
        fr.setResizable(false);      // By default frame window is resizable
        fr.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e)
            {
                System.exit(0);
            }

        });
    }
}     

Output -


Or we can also use Dimension class to create the same frame -
import java.awt.*;
import java.awt.event.*;
public class Test 
{
    public static void main(String args[])
    {
        Frame fr = new Frame("My Frame");
        fr.setVisible(true);
        Dimension dim = new Dimension(400, 400);
        fr.setSize(dim);
        fr.setResizable(false);
        fr.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e)
            {
                System.exit(0);
            }

        });
    }
}    
Or we can also use default constructor to create frame -
import java.awt.*;
import java.awt.event.*;
public class Test extends Frame
{
    public Test()
    {
        super("My Frame"); 
 // set the frame title by calling superclass (Frame) constructor
        setSize(400, 400);
        setResizable(false);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e)
            {
                System.exit(0);
            }

        });
    }
    public static void main(String args[])
    {
        Test t1 = new Test();
        t1.setVisible(true);
    }
}
    

Creating Frame of the same size as of your Desktop/Laptop Screen Size -

import java.awt.*;
import java.awt.event.*;
public class Test extends Frame
{
    public Test()
    {
        super("My Frame");  // set the frame title by calling superclass (Frame) constructor
     Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
     // return the screen size as Dimension object
        setSize(dim);
        setResizable(false);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e)
            {
                System.exit(0);
            }

        });
    }
    public static void main(String args[])
    {
        Test t1 = new Test();
        t1.setVisible(true);
    }
}
    

Panel -

Panel class is used to create a container windows, it is a direct subclass of Container class and Parent class of Applet. i.e. Applet output is drawn on Panel surface.
Panel window doesn't have any title bar, menu bar or border. The default layout of Panel is Flow Layout.
It has following two constructors -
public panel()
It creates a panel with default layout (Flow Layout).   
public panel(LayoutManager layout)
It creates a panel with the specified layout.

Adding Component in panel -

We can add component in panel window by using add() method of java.awt.Container class.
There are several version of add() is available but the simplest and most commonly used one is -
public Component add(Component cmpt)
It adds the Component cmpt at the end of container (Panel in our case) and also returns the same.

Example -

In this example we create panel that is having 4 components- 2 labels and 2 text fields. Finally we place the panel in frame window.
import java.awt.*;
import java.awt.event.*;
public class Test
{
    public static void main(String args[])
    {
        Frame f1 = new Frame();
        f1.setSize(400, 400);
        f1.setVisible(true);
        Panel p1 = new Panel();
        Label l1 = new Label("User Name:");
        Label l2 = new Label("Password:");
        TextField t1 = new TextField(25);
        TextField t2 = new TextField(25);
        
        p1.add(l1);   // adding component in panel
        p1.add(t1);
        p1.add(l2);
        p1.add(t2);
        f1.add(p1);      // adding panel in frame
    }
}
    

Output -


ADS

ADS