Simple Graphical
User Interface 5
Getting into the 'swing' of Java GUI's:
This 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.
Source code.
Let's have a closer look.
import java.awt.*; import java.awt.event.*; import jm.JMC; import jm.music.data.*; import jm.util.*; import javax.swing.*;
|
The swing classes as part of the javax (extended) group of
packages.
public class SimpleGUI extends JFrame implements ActionListener, JMC{ private int musicNumber = 0; private JButton composeBtn; public static void main(String[] args) { new SimpleGUI(); }
|
The swing version of the frame and button classes are used.
public SimpleGUI() { super("A simple GUI interface for jMusic"); composeBtn = new Button("Compose"); composeBtn.addActionListener(this); this.getContentPane().add(composeBtn); this.pack(); this.setVisible(true); }
|
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.
public void actionPerformed(ActionEvent ae){ if (ae.getSource() == composeBtn) { makeMusic(); musicNumber++; System.out.println("The button has been pressed " + musicNumber + " times"); } }
|
No changes to the event handling, the awt classes are still
used for this.
public void makeMusic() { }
|
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. |