Lesson Overview
This lesson introduces graphical user interface (GUI) programming in Java using Swing. It covers frames, common GUI components, layouts, event handling, lists, combo boxes, panels, and menus.
GUI Components
A GUI component is an on-screen object that users interact with through the mouse, keyboard, or another input device.
Common Swing components:
| Component | Purpose |
|---|---|
JFrame | Main window for a desktop GUI program |
JLabel | Displays text or an image that the user cannot directly edit |
JTextField | Accepts one line of user input |
JButton | Runs code when clicked |
JCheckBox | Represents an option that can be selected or cleared |
JRadioButton | Represents one option in a group of choices |
JComboBox | Displays a drop-down list |
JList | Displays a list of selectable items |
JPanel | Groups components inside a container |
JMenuBar, JMenu, JMenuItem | Build menus |
Swing Overview
Swing classes are found mainly in javax.swing. Many Swing components are lightweight, meaning they are drawn and managed mostly by Java rather than relying completely on the operating system's native widgets.
Swing GUI classes build on Java's class hierarchy. For example, JFrame is a window, and many components inherit shared behavior from classes such as Component and Container.
JFrame Basics
A JFrame is a top-level window. Common frame tasks include setting the title, size, layout, close operation, and visibility.
Activity: Basic JFrame
Labels and Text Fields
JLabel displays text or icons. JTextField lets the user type a single line of text.
Activity: Label and Text Field
Buttons and Action Events
A JButton can respond to clicks using an ActionListener. The listener's actionPerformed() method runs when the button is clicked.
Activity: Button Click Event
Check Boxes and Radio Buttons
A JCheckBox lets users choose independent options. A JRadioButton is usually placed in a ButtonGroup so only one option in the group can be selected at a time.
Activity: Check Box and Radio Button
Combo Boxes and Lists
JComboBox is useful for choosing one item from a drop-down list. JList displays several items at once and can allow single or multiple selections.
Activity: Combo Box
Activity: JList
Layout Managers
A layout manager controls how components are placed inside a container.
Common layout managers:
| Layout | Behavior |
|---|---|
FlowLayout | Places components left to right, wrapping as needed |
BorderLayout | Places components in NORTH, SOUTH, EAST, WEST, and CENTER regions |
GridLayout | Places components in equal-sized rows and columns |
Activity: JPanel with Layouts
Menus with Frames
Swing menus are built with JMenuBar, JMenu, and JMenuItem. Menus can also contain separators, radio button menu items, and checkbox menu items.
A menu item can respond to clicks using an ActionListener.
Activity: Basic Menu
Laboratory Exercises
Try It: Swing Structure
This sandbox cannot display GUI windows — run this code locally in your IDE to see the frame. The example below prints what would render.