JFrame Class

Package: javax.swing

The top-level component of most Swing-based applications is a “frame” and is defined by the JFrame class. By itself, a frame doesn’t do much, but to do anything else in Swing, you must first create a frame. Figure 5-6 shows a frame that does nothing but display the message Hello, World! in its title bar.

9781118239742-fg0506.tif

Figure 5-6

Constructors

Constructor

Description

JFrame()

Creates a new frame with no title

JFrame(String title)

Creates a new frame with the specified title

Methods

Method

Description

void add(Component c)

Adds the specified component to the frame.

JMenuBar getJMenuBar()

Gets the menu for this frame.

void pack()

Adjusts the size of the frame to fit the components you added to it.

void remove(Component c)

Removes the specified component from the frame.

void setDefaultClose Operation

Sets the action taken when the user closes the frame. You should almost always specify JFrame.EXIT_ON_CLOSE.

void setIconImage(Icon image)

Sets the icon displayed when the frame is minimized.

void setLayout(Layout Manager layout)

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

void setLocation(int x, int y)

Sets the x and y positions 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.