Lesson 8: Introducing the GUI with Swing
Yakov Fain · 2015
This chapter describes the principles of building graphic user interfaces (GUIs) while developing a simple desktop calculator using the Java Swing library. Swing classes are located in the javax.swing package, and the process of creating a UI comes down to extending some of these classes to display the UI and respond to various user- and system-generated events. You create a top-level window with a title and border by instantiating the class JFrame. Even though you can enforce exact coordinates and sizes for each window component, Swing has layout managers that can maintain relative positions for all controls without assigning strict positions to them. The chapter explains the following layout managers: FlowLayout, GridLayout, BoxLayout, BorderLayout, CardLayout, and GridBagLayout. To use any layout manager, instantiate it first and then assign this instance to a container via setLayout().