Page 772 - Introduction to Programming with Java: A Problem Solving Approach
P. 772
738 Chapter 17 GUI Programming—Component Layout, Additional GUI Components
This adds the slider to the current JFrame:
add(slider, BorderLayout.SOUTH);
If you’d like to see Figure 17.21’s program in its entirety, view the EclipseSlider.java file on the book’s Web site.
Summary
• Layout managers automate the positioning of components within containers.
• The FlowLayout class implements a simple one-compartment layout scheme that allows multiple
components to be inserted into the compartment.
• The BorderLayout manager provides five regions/compartments—north, south, east, west, and
center—in which to insert components.
• The SwingConstants interface stores a set of GUI-related constants that are commonly used by
many different GUI programs.
• The GridLayout manager lays out a container’s components in a rectangular grid of equal-sized
cells. Each cell can hold only one component.
• If you have a complicated window with many components, you might want to compartmentalize them
by storing groups of components in JPanel containers.
• To display multiple lines of text, use a JTextArea component.
• A JCheckBox component displays a small square with an identifying label. Users click the check box Apago PDF Enhancer
in order to toggle it between selected and unselected.
• A JRadioButton component displays a small circle with a label to its right. If an unselected but-
ton is clicked, the clicked button becomes selected, and the previously selected button in the group
becomes unselected.
• A JComboBox component allows the user to select an item from a list of items. JComboBox compo-
nents are called “combo boxes” because they are a combination of a text box (normally, they look just like a text box) and a list (when the down arrow is clicked, they look like a list).
Review Questions
§17.2 GUI Design and Layout Managers
1. Layout managers adapt automatically to changes in the size of a container or one of its components. (T / F) 2. Which package contains layout managers?
§17.3 FlowLayout Manager
3. How does the FlowLayout manager arrange components?
4. Write a single statement that gives the current container a flow layout with right alignment.
§17.4 BorderLayout Manager
5. What are the five regions established by the BorderLayout manager?
6. The sizes of the five regions in a border layout are determined at runtime based on the contents of the four
outer regions. (T / F)
7. By default, how many components can you put in any one region of a border layout?
8. Write a single statement that adds a new JLabel with the text “Stop” to the center region of a
BorderLayout manager. The label should be centered within the center region.