Creating a Swing
Download
Report
Transcript Creating a Swing
Chapter 9: Visual
Programming Basics
Object-Oriented Program
Development Using Java:
A Class-Centered
Approach
Objectives
Event-Based Programming
Creating a Swing-Based Window
Adding a Window Closing Event Handler
Adding a Button Component
Common Programming Errors
2
Event-Based Programming
Event-based programs provide fully
functioning GUIs
An event is initiated by a user action
A program must:
Correctly assess which specific event has
occurred
Provide the appropriate code to perform an action
based on the identified event
3
4
Event-Based Programming (continued)
Actions that trigger events include:
Placing the mouse pointer over a button and
clicking the left mouse button
Using the TAB key until the desired button is
highlighted with a dotted line then pushing the
Enter key
Pressing an accelerator key (shortcut key)
The sequence of events in a program are
controlled by the user
5
Event-Based Programming
(continued)
Programmer provides:
Code to create GUI
Code to appropriately process events
Java provides a set of objects for coding
GUIs:
AWT (abstract window toolkit)
Older GUI components
Swing
Newer GUI components
6
7
Swing Components
Examples
Top-Level Container – JFrame, JApplet,
JWindow
Intermediate Container – JPanel,
JInternalFrame
Atomic – JButton, JCheckBox, JTextField
8
The Event-Based Model
Operating system:
Has total control of computer
Never relinquishes control to any executing
programs
Most executing programs spend the
majority of their time in a sleep type of
mode
When an event occurs:
The operating system passes event
information to the appropriate application
Permits the application to take action
9
Containment Hierarchy
Hierarchy of component placement
Consists of one and only one top-level container
Any number of other intermediate containers
And/or atomic components
JFrame is most commonly used as a top-level
container
Heavyweight components (earlier Java term = toplevel containers) are responsible for interfacing with
the operating system
10
Containment Hierarchy (continued)
A content pane (next level of hierarchy) is an
internal component provided by each toplevel container
All of the visible components displayed by a
GUI must be placed on a content pane
Menu bar:
Can be optionally added to a top-level container
Placed outside of a content pane
11
Containment Hierarchy
(continued)
Layout manager:
Defines how components are positioned and
sized within a container’s content pane
Default placement can always be changed
By explicitly specifying another layout manager
Lightweight components:
Intermediate containers and atomic components
Do not interface with the operating system
12
JFrame automatically
provides a root pane, which
in turn contains the content
pane. Generally, not
concerned with root pane.
13
Layout managers
Each top and intermediate-level containers has
a default layout manager that defines the
components position and size withing the
container’s content pane. You can always
change the default layout by specifying
another layout manager (chapter 10).
The six layout managers currently available
are:
14
15
Lightweight Components
intermediate containers and atomic
components
do not interface directly with the operating
system (hence lightweight)
16
17
Creating a Swing-Based
Window
Two predominant approaches:
Construct a GUI as a separate class using Swing
components
Construct a GUI object using Swing components
from within the main() method
18
Creating a Swing-Based
Window (continued)
Create JFrame:
JFrame mainFrame = new JFrame("First GUI Window");
Setting size:
syntax: objectReferenceName.setSize(width, height)
mainFrame.setSize(300,150);
19
20
Could also use a single statement
jFrame mainFrame = new JFrame(“First Gui Window”);
21
Creating a Swing-Based
Window (continued)
Display JFrame:
Use show()
Or setVisible(true)
22
import javax.swing.*;
public class FirstWindow extends JFrame
{
private JFrame mainFrame;
public FirstWindow() // a constructor
{
mainFrame = new JFrame("First GUI Window");
Provides access to
all public and
protected methods
in the JFrame class
mainFrame.setSize(300,150);
mainFrame.show();
}
public static void main(String[]args)
{
new FirstWindow();
}
} // end of class
23
Look and Feel
Refers to:
How a GUI appears on screen
How a user interacts with it
Swing package:
Supports four look and feel types
If no look and feel is specified, the default Java
look and feel is used
24
To implement a specific look and feel, this code must be
contained in a try and catch block.
25
26