JPanel Class

Package: javax.swing

The JPanel class defines a panel, which is a type of container designed to hold a group of components so they can be displayed in a frame. The normal way to display a group of controls — text fields, labels, buttons, and other GUI widgets — is to add those controls to a panel and then add the panel to the frame. You can bypass the panel and add the controls directly to the frame, but using a separate panel to hold the frame’s controls is almost always a good idea.

Constructors

Constructor

Description

JPanel()

Creates a new panel.

JPanel(boolean isDoubleBuffered)

Creates a new panel. If the parameter is true, the panel uses a technique called double buffering, which results in better display for graphics applications. This constructor is usually used for game programs or other panels that display animations.

JPanel(LayoutManager layout)

Creates a new panel with the specified layout manager. The default layout manager is FlowLayout.

Methods

Method

Description

void add(Component c)

Adds the specified component to the panel.

void remove(Component c)

Removes the specified component from the panel.

void setLayout (LayoutManager layout)

Sets the layout manager used to control how components are arranged when the panel is displayed. The default is the FlowLayout manager.

void setLocation(int x, int y)

Sets the x and y position of the frame onscreen. The top-left corner of the screen is ...

Get Java For Dummies Quick Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.