§ — — Object-Oriented Programming with Java
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.
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 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.
A JFrame is a top-level window. Common frame tasks include setting the title, size, layout, close operation, and visibility.
ProReviewer — locked
Drills, code labs, and full solutions.
JLabel displays text or icons. JTextField lets the user type a single line of text.
ProReviewer — locked
Drills, code labs, and full solutions.
A JButton can respond to clicks using an ActionListener. The listener's actionPerformed() method runs when the button is clicked.
ProReviewer — locked
Drills, code labs, and full solutions.
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.
ProReviewer — locked
Drills, code labs, and full solutions.
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.
ProReviewer — locked
Drills, code labs, and full solutions.
ProReviewer — locked
Drills, code labs, and full solutions.
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 |
ProReviewer — locked
Drills, code labs, and full solutions.
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.
ProReviewer — locked
Drills, code labs, and full solutions.
ProReviewer — locked
Drills, code labs, and full solutions.
This sandbox cannot display GUI windows — run this code locally in your IDE to see the frame. The example below prints what would render.
Done with this module? Track it — your progress shows on the subject list.
Up next
Database and JDBC→←Previous: Prewritten Classes and Methods