Java Reference
In-Depth Information
Summary
Section 12.1 Introduction
• A graphical user interface (GUI; p. 474) presents a user-friendly mechanism for interacting with
an application. A GUI gives an application a distinctive look-and-feel (p. 474).
• Providing different applications with consistent, intuitive user-interface components gives users
a sense of familarity with a new application, so that they can learn it more quickly.
• GUIs are built from GUI components (p. 474)—sometimes called controls or widgets.
Section 12.2 Java's Nimbus Look-and-Feel
• As of Java SE 6 update 10, Java comes bundled with a new, elegant, cross-platform look-and-feel
known as Nimbus (p. 476).
• To set Nimbus as the default for all Java applications, create a
swing.properties
text file in the
lib
folder of your JDK and JRE installation folders. Place the following line of code in the file:
swing.defaultlaf=com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel
• To select Nimbus on an application-by-application basis, place the following command-line ar-
gument after the java command and before the application's name when you run the application:
-Dswing.defaultlaf=com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel
Section 12.3 Simple GUI-Based Input/Output with
JOptionPane
• Most applications use windows or dialog boxes (p. 476) to interact with the user.
• Class
JOptionPane
(p. 476) of package
javax.swing
(p. 474) provides prebuilt dialog boxes for
both input and output.
JOptionPane
static
method
showInputDialog
(p. 477) displays an input
dialog (p. 476).
• A prompt typically uses sentence-style capitalization—capitalizing only the first letter of the first
word in the text unless the word is a proper noun.
• An input dialog can input only input
String
s. This is typical of most GUI components.
•
JOptionPane
static
method
showMessageDialog
(p. 478) displays a message dialog (p. 476).
Section 12.4 Overview of Swing Components
• Most Swing GUI components (p. 474) are located in package
javax.swing
.
• Together, the appearance and the way in which the user interacts with the application are known
as that application's look-and-feel. Swing GUI components allow you to specify a uniform look-
and-feel for your application across all platforms or to use each platform's custom look-and-feel.
• Lightweight Swing components are not tied to actual GUI components supported by the under-
lying platform on which an application executes.
• Several Swing components are heavyweight components (p. 480) that require direct interaction
with the local windowing system (p. 480), which may restrict their appearance and functionality.
• Class
Component
(p. 480) of package
java.awt
declares many of the attributes and behaviors com-
mon to the GUI components in packages
java.awt
(p. 479) and
javax.swing
.
• Class
Container
(p. 480) of package
java.awt
is a subclass of
Component
.
Component
s are attached
to
Container
s so the
Component
s can be organized and displayed on the screen.
• Class
JComponent
(p. 480) of package
javax.swing
is a subclass of
Container
.
JComponent
is the su-
perclass of all lightweight Swing components and declares their common attributes and behaviors.
• Some common
JComponent
features include a pluggable look-and-feel (p. 480), shortcut keys
called mnemonics (p. 480), tool tips (p. 480), support for assistive technologies and support for
user-interface localization (p. 480).