Introduction
to Java/Swing
Java
is commonly used for deploying applications across a network. Compiled Java
code may be distributed to different machine architectures, and a native-code
interpreter on eacharchitecture interprets the Java code. The core functions
found in the Java interpreter are called the JFC (Java Foundation Classes). JFC
provides generally useful classes, including classes for GUIs, accessability
and 2D drawing. The original GUI classes in Java are known as AWT – the Abstract
Windowing Toolkit. AWT provides basic GUI functions such as buttons, frames and
dialogs, and is implemented in native code in the Java interpreter.
By
contrast, Swing is not implemented in native code - instead it is implemented
in AWT. Swing and AWT can (and normally do) coexist - we may use the buttons
from Swing, alongside AWT event handlers.
·
A GUI component framework
o
A set of Java classes and interfaces
for creating graphical interfaces
o
A follow-on to AWT
·
The Swing API provides:
o
A rich set of predefined components
o
A basis for creating sophisticated
custom components
·
More flexible than AWT
·
Lets you:
o Create non-rectangular components#
o Combine components
o Customize look & feel (L&F)
·
Sophisticated built-in features:
o Tooltips
o Borders and insets
o Double-buffering for cleaner displays
o Slow-motion graphics for debugging
·
Additional Swing APIs for:
o Sophisticated text editing (e.g. HTML, RTF)
o Undo/redo
The
advantages of Swing are:
1.
Consistent look-and-feel - The look and feel is consistent across platforms.
2.
Pluggable look-and-feel - The look and feel can be switched on-the-fly.
3.
High-level widgets - the Swing components are useful and flexible.
In
general, the Swing components are easier to use than similar AWT components.
Swing
programming
In this course I
hope to clarify the general style of Swing applications, and show sufficient
examples to build menu’d GUI applications with interesting graphical
interactions. The same strategy was used in the introduction to Tcl/Tk. A good
book that covers this material in detail is
The JFC Swing Tutorial, by KathyWalrath and Mary
Campione.
The
toplevel components provided by Swing are:
1.
JApplet - for applets within web pages
2.
JDialog - for dialog boxes
3.
JFrame - for building applications
All
other Swing components derive from the JComponent class. JComponent provides
Tool tips -
little windows with explanations
Pluggable
look and feel - as described
Layour
management - items within the component
Keyboard
action management - Hot keys and so on.
And
other facilities
Swing implements
MVC architecture.
Pluggable look
and feel
It
is relatively easy to change the look and feel of an application - here are
three:
If
you wished to use the WinXX look-and-feel, in the main of your application, you
can make
the
following call:
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
·
Hello World applet
o Ordinary Applet
o Contains Swing label, textfield & button
·
Illustrates:
o Swing replacements for AWT components
o Swing components added to an Applet
o ToolTips
An
equivalent Hello-world applet:
This code
follows the same structure - it just instantiates a JLabel, and sets the
text field, although in this code, the class extends a JApplet instead
of a JFrame. When we compile and run this application we get a HelloWorldApp.class
file, which has to be referenced in a web page:
The
end result is:
For all java concepts online training please contact : training@virtualnuggets.com
No comments:
Post a Comment