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.
What is Swing?
- A GUI component framework
- A set of Java classes and interfaces for creating graphical interfaces
- A follow-on to AWT
- The Swing API provides:
- A rich set of predefined components
- A basis for creating sophisticated custom components
Why Swing?
- More flexible than AWT
- Lets you:
- Create non-rectangular components#
- Combine components
- Customize look & feel (L&F)
- Sophisticated built-in features:
- Tooltips
- Borders and insets
- Double-buffering for cleaner displays
- Slow-motion graphics for debugging
- Additional Swing APIs for:
- Sophisticated text editing (e.g. HTML, RTF)
- Undo/redo
The advantages of Swing are:
- Consistent look-and-feel - The look and feel is consistent across platforms.
- Pluggable look-and-feel - The look and feel can be switched on-the-fly.
- 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:
- JApplet - for applets within web pages
- JDialog - for dialog boxes
- JFrame - for building applications
- 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");
A Simple Swing Applet
- Hello World applet
- Ordinary Applet
- Contains Swing label, textfield & button
- Illustrates:
- Swing replacements for AWT components
- Swing components added to an Applet
- 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:
No comments:
Post a Comment