– Prior to the introduction of the Swing package, the Abstract Window Toolkit (AWT) components provided all the UI components in the JDK 1.0 and 1.1 platforms.... Swing Toolkit.[r]
(1)CÔNG NGHỆ JAVA
CH9 GUI PROGRAMMING - SWING
Quang Dieu Tran PhD.
(2)What is a SWING
• Swing is the next-generation GUI toolkit that Sun
Microsystems created to enable enterprise
development in Java.
• Swing is part of a larger family of Java products
known as the Java Foundation Classes ( JFC)
• The Swing package was first available as an add-on
to JDK 1.1.
(3)Swing Toolkit
• 100% Java implementation of components
• Pluggable Look & Feel
– customizable for different environments, or
– use Java Look & Feel in every environment
• Lightweight components
– no separate (child) windows for components
– allows more variation on component structure
– makes Look & Feel possible
• Three parts
– component set (subclasses of JComponent)
– support classes
(4)(5)Swing Overview
• Swing Components and the Containment Hierarchy
– Swing provides many standard GUI components such as buttons, lists, menus, and text areas, windows, tool bars
• Layout Management
– Containers use layout managers to determine the size and position of the components they contain
• Event Handling
– Event handling is how programs respond to external events, such as the user pressing a mouse button
• Painting
– Painting means drawing the component on-screen
– it's easy to customize a component's painting • More Swing Features and Concepts
(6)import java.awt.*; import javax.swing.*;
public class HelloAppTest {
public static void main(String[] args) { EventQueue.invokeLater(new Runnable() {
public void run() {
HelloFrame frame = new HelloFrame("Hello World App"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
} }); } }
class HelloFrame extends JFrame {
public HelloFrame(String title) { super(title);
JLabel label = new JLabel("Hello World!");
getContentPane().add(label);
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
}
public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 200;
}
(7)Walk through this program
• What to import
– You need to import both java.awt.* and javax.swing.* to get all the basic classes
import java.awt.*;
import javax.swing.*;
HelloFrame frame = new HelloFrame("Hello World App");
• Create Frame
–Creating a JFrame gives you a "top level" container •A top-level window
(8)Adding component
• The Content Pane
– components must add to the frame’s content cane
– Call getContentPane() to get the content pane
• Adding content
– Create another Swing component
• In this case, a label is created with the text "Hello, World!"
– Call add() with the component as a parameter to add it to the frame’s content pane
(9)Resizing and showing
• setSize(width, height)
• pack() frame to the minimum size needed to display the items in the content pane.
• Calling setVisible(true) will show the
JFrame.
– You must set the size of the frame (directly or
with pack()) before you show the frame.
frame.pack();
(10)(11)Swing Components Example
• SwingApplication example creates four commonly used Swing components:
– a frame, or main window (JFrame)
– a panel, sometimes called a pane (JPanel)
– a button (JButton)
– a label (JLabel)
frame
panel button
(12)Swing Container
• The frame (JFrame) is a top-level container
– Provide a place for other components to paint themselves
– The other commonly used top-level containers are dialogs (JDialog) and applets (JApplet)
• The panel (JPanel) is an intermediate container.
– Used to group small lightweight components together
– Other intermediate Swing containers, such as scroll panes (JScrollPane) and tabbed panes (JTabbedPane),
(13)Swing Atomic Components
• The button and label are atomic components
– components that exist as self-sufficient entities that present bits of information to the user
• Often, atomic components also get input from the
user
• The Swing API provides many atomic components
– Button (JButton)
– Combo boxes (JComboBox),
– Text fields (JTextField),
– Tables (JTable)
(14)The Containment Hierarchy • A diagram of the containment hierarchy shows each
container created or used by the program, along with the components it contains
SwingApplication
Even the simplest Swing
program has multiple levels in its containment hierarchy The root of the containment hierarchy is always a
(15)(16)Top-Level Containers
(17)General-Purpose Containers • Intermediate containers that can be used
under many different circumstances
JPanel
JScrollPane
JTabbedPane JSplitPane
(18)Special-Purpose Containers
JInternalFrame
(19)Basic Controls - Buttons
• Basic Controls: Atomic components that exist primarily to get input from the user; they
(20)Basic Controls - MENUS