Open In App

Java AWT Panel

Last Updated : 13 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In Java’s Abstract Window Toolkit (AWT), the Panel class is a fundamental component for creating graphical user interfaces. It offers a straightforward way to organize and group various GUI elements. This article explores the Panel class in Java AWT, covering its essential aspects, methods, and constructors, and demonstrating its practical use through example code.

Class Declaration

public class Panel extends Container

The panel is a subclass of the Container class, which means it can contain other AWT components.

Constructors of AWT Panel

Constructor

Description

Panel()

Creates a new panel with the default layout manager.

Panel(LayoutManager layout)

Creates a new panel with the specified layout manager.

Methods of AWT Panel

Method

Description

void add(Component comp)

Adds the specified component to this panel.

Component getComponent(int n)

Returns the component at the specified index.

void remove(Component comp)

Removes the specified component from this panel.

void removeAll()

Removes all components from this panel.

void setLayout(LayoutManager layout)

Sets the layout manager for this panel.

Example of AWT Panel

Below is the implementation of the above topic:

Java




// Java Program to implement 
// AWT Panel
import java.awt.*;
import java.awt.event.*;
  
// Driver Class
public class PanelExample {
      // main function
    public static void main(String[] args) {
        
        Frame frame = new Frame("Java AWT Panel Example");
        Panel panel1 = new Panel();
        Panel panel2 = new Panel();
  
        // Set the layout manager for panel1
        panel1.setLayout(new FlowLayout());
  
        // Add components to panel1
        Button button1 = new Button("Button 1");
        Button button2 = new Button("Button 2");
        
        panel1.add(button1);
        panel1.add(button2);
  
        // Set background colors for panels
        panel1.setBackground(Color.CYAN);
        panel2.setBackground(Color.MAGENTA);
  
        // Add panels to the frame
        frame.add(panel1);
        frame.add(panel2);
  
        frame.setSize(400, 200);
        frame.setLayout(new FlowLayout());
        frame.setVisible(true);
        
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent we) {
                System.exit(0);
            }
        });
    }
}


In this example, we create two panels (panel1 and panel2) and add buttons to panel1. The setLayout method is used to specify the layout manager for panel 1, and we set different background colors for the panels. The Frame class is used to create the main window for the GUI.

Output:

Demo Video of the Example

Output Screen:

Output window of the Java AWT PanelConclusion

The Panel class in Java AWT is a versatile tool around for designing well-structured graphic user interfaces. By allowing the grouping of components within panels, it simplifies and enhances the GUI plan. This article introduced the Panel class, its methods, and constructors, and provided an informative model. Armed with this knowledge, you can in effect purchase the Panel class to make more unionized and visually pleasing Java applications.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads