Docsity

Understanding Swing: A Java GUI Package, Slides of Java Programming

Partial preview of the text

Download Understanding Swing: A Java GUI Package and more Slides Java Programming in PDF only on Docsity!

Swinging Into Swing

Understanding what Swing Is

• Swing is a package that lets you create

applications that use a flashy Graphical User

Interface (or GUI) instead of a dull console

interface.

• The Swing API provides many different classes

for creating various types of user interface

elements.

The Swing Class Hierarchy

Description of Classes

  • Object: All classes ultimately derive from Object, thus this class is at the top of the tree.
  • Component: represents an object that has a visual representation that can be shown on-screen and that can interact with users. This class defines some basic methods that are available to all Swing classes.

Description of Classes (Cont’d)

  • Frame: a type of Window that serves as the basis for Java GUI applications. Frame is an AWT class that has been improved upon by the JFrame class.
  • JFrame: the Swing version of the older Frame class. Most of the Swing applications include at least one JFrame object.
  • JComponent: is the basis for all other Swing components except for frames.

Description of Classes (Cont’d)

• JPanel: used to organize and control the layout

of other components such as labels, buttons,

text fields, etc. In most Swing applications, one

or more panels are added to a frame. Then,

when the frame is displayed, the components

that were added to its panels are made visible.

• JLabel: creates a label that displays a simple

text value.

Useful JFrame Constructors and

Methods (Cont’d)

Method Description JMenuBar getJMenuBar ( ) (^) Gets the menu for this frame. void pack ( ) (^) Adjusts the size of the frame to fit the components added to it. void remove (Component c) (^) Removes the specified component from the frame.

Useful JFrame Constructors and

Methods (Cont’d)

Method Description void remove (Component c) (^) Removes the specified

component from the frame. void setDefaultCloseOperation (^) Sets the action taken when

the user closes the frame. Always specify JFrame.EXIT ON CLOSE.

Useful JFrame Constructors and

Methods (Cont’d)

Method Description void setLocation (int x, int y)

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

void setLocationRelativeTo (Component c)

Centers the frame on-screen if the parameter is null.

Useful JFrame Constructors and

Methods (Cont’d)

Method Description void setResizeable (boolean value)

Sets whether or not the size of the frame can be changed by the user. The default setting is true (the frame can be resized).

Using the JPanel Class

  • A panel is a type of container that's designed to hold a group of components so they can be displayed on a frame. The normal way to display a group of controls such as 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 if you want, but using a separate panel to hold the frames control is almost always a good idea.

Useful JPanel Constructors and

Methods

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.

Useful JPanel Constructors and

Methods (Cont’d)

Method Description

void add (Component c) Adds the specified

component to the panel.

void remove (Component c) Removes the specified

component from the

panel.

Useful JPanel Constructors and

Methods (Cont’d)

Method Description

void setLayout (LayoutManager layout)

Sets the layout manager used to

control how components are

arranged when the panel is

displayed. The default is the

FIowLayout manager.