Graphical User Interfaces with Swing

"It don't mean a thing if it ain't got that Swing"
                             -- Duke Ellington

We want to:

We will only use Swing for GUI components - we will not use the Abstract Window Toolkit (AWT).
We will use AWT event handling, but no AWT components.

Some Definitions and Terminology

How to Create a GUI

So to create our first GUI we will:

Example: Adding a panel (instance of JPanel) to the content pane of our frame (instance of JFrame).
JFrame frame = new JFrame("Our First Frame");
Container contentPane = frame.getContentPane(); // returns content pane object for this JFrame
JPanel p = new JPanel(); // we will add low level components like buttons to this panel

Two additional comments:

Event Handling - The Basics

Different event sources produce different event objects. A button sends ActionEvent objects, a window sends WindowEvent objects.

We will first handle making our frame closeable - we need some way of being notified that the user is closing the frame:

The listener we will use: an instance of a class that implements the WindowListener interface. The WindowListener interface contains 7 methods, and we are interested in this one:
public void windowClosing(WindowEvent e)
We will implement this method so that the program exits when it is executed.

Using an (anonymous) class that implements the WindowListener Interface:
To avoid implementing all 7 methods in the WindowListener interface, we will use a class WindowAdapter that implements this interface. The WindowAdapter class implements all 7 methods of WindowListener interface to do nothing - we will write a new class that extends the WindowAdapter class and overrides the windowClosing() method.

frame.addWindowListener(new WindowAdapter()
{ // definition of anonymous class that extends WindowAdapter
   public void windowClosing(WindowEvent e)
});  // Our anonymous inner class extends the WindowAdapter class. Now the program terminates when the
        // frame is closed.

Example: FirstGUI class

The Inheritance Hierarchy for GUI Components (in class)
 -- Methods that are used to resize and reshape frames are in the Component class and the Window class.

Example: Another GUI. This time we will:

Layout Management

Flow Layout

Component pane = JPanel();
pane.setLayout(new FlowLayout(FlowLayout.LEFT));  // pass in alignment to
     // FlowLayout constructor

Example: Multiple Buttons in a panel.

Example: GUIEx - edit it to use a different alignment

Another FlowLayout constructor:
FlowLayout(int alignment, int horizontalGap, int verticalGap)

Border Layout

: pane.setLayout(new BorderLayout() );
pane.add(button, "South");

Exercise: Modify GUIEx so that button, button2, button3 appear in the South region of the frame.

Grid Layout

Example: JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2, 2));

Exercise: Write your own program that uses a grid layout to place 2 rows of buttons in a frame, with 2 buttons per row. Handle button clicks for each button.