Getting into the 'swing' of Java GUI's:Simple Graphical User Interface 5This tutorial shows how to implement the swing libraries. The 'swing' libraries are an extension of the Java awt graphical libraries. Rather than relying on the operating system graphical elements, the swing libraries draw all the GUI components in Java itself. As a reult the swing components are caleed 'light weight' components. This means little to the programmer, except that a swing GUI can look the same on any platform, or it can take on the look and feel of the host operating system. Also, the swing libraries are more fully featured than the awt in many cases and a number of drawing details (such as double buffering and window closing) are dealt with automatically in swing components. Each swing class that is equivalent to an awt class has the same name but with a J in front. For example the swing version of the Frame class is the JFrame class. Below is the GUI program we encountered in GUI tutorial 1, but this time using the swing library. Let's have a closer look.
The swing classes as part of the javax (extended)
group of packages.
The swing version of the frame and button classes
are used.
A JFrame is a more complex beast than the awt Frame which is why it provides us with more built-in functionality, but it also adds some complexity such as requiring us to add components to its 'content pane' rather than directly to the frame itself. The show keyword has been replaced with
setVisible() in line with the policy of using get and set method names
as consistently as possible from java versions 1.1 onward.
No changes to the event handling, the awt classes
are still used for this.
Of course there is no change to the jMusic code either. That wasn't so hard was it :) When you run this code on your machine it should take on the default 'metal' look rather than the look of your native window system (unless you run a system such as Mac OS X which defaults to its own - more pretty - visual appearance). The swing libraries are much more complete than the awt, with components such as the JTabbedPane and so on. We suggest you use the swing libraries for the most part, and check out a good book (or the Sun web site) to familiarise yourself with the swing components. |