Graphical User Interfaces
A Graphical User Interface allows a user to enter data using a screen of graphical components,
rather than a series of prompts, like a character-based interface.
The user can fill out the data in the order they wish, and the GUI reacts in real-time.
For instance, often when a user presses a button, the data is processed.
This type of programming is called Event-Driven.
To create a graphical user interface (GUI, pronounced goo-ee), you will:
- Create a Frame.
- Decide on the components (buttons, labels, check boxes, ...) you want.
- Decide on the layout manager, which controls how components are shown on the screen (more below).
- Set the layout manager.
- Create each component (with new).
- Add each component to the Panel or Frame using add().
- Handle the event associated with the components in your GUI
(more in Basic Event Handling).
- Note: the basic setup of creating and adding the Components and Event Handlers for a GUI is done only once.
- HelloWorldSwing.java example from Oracle.
- Important: Don't focus on the Threading shown in the GUI examples; just make sure to
follow the pattern demonstrated by the code.
Swing
- Swing is the set of Java classes for components used in GUI development.
- Rumor has that the name does come from the music - they were listening to it at a Java conference.
- The Swing classes are part of the Java Foundation Library (the javax package).
- The swing classes start with J - for instance JButton.
Components
Components are the basic building-blocks for creating a Gui.
Layout Managers
Layout Managers control how Components are laid out on the screen.
RunGUI application
The
RunGUI.java doesn't do anything (so no event handling).
It runs one of the examples GUIs:
- FlowGUI
- GridGUI
- BorderGUI
Here is the abstract class used by the 3 GUIs:
Here is the BetterGUI that uses multiple layout managers:
Each of the GUIs just demonstrates using the layout manager to add components.
Resources
The Swing Tutorial (Oracle Tutorial)
Lesson: Java Applets
(Oracle Tutorial) - Optional, Applets have been deprecated.