The system calls the appropriate methods in the layout manager when necessary. Once you have created a LayoutManager object and told the container to use it (by calling setLayout()), you're finished with it. You will never directly call the methods of the LayoutManager interface for the most part, layout managers do their work behind the scenes. It is the LayoutManager’s duty to determine the position and size of each component within the Container. The LayoutManager interface defines the responsibilities of something that wants to lay out Components within a Container. We will also discuss how to combine layouts to generate more complex screens and how to create your own LayoutManager for special situations. This chapter discusses all of them, along with the LayoutManager and LayoutManager2 interfaces we'll pay particular attention to how each layout manager computes positions for its components. The VariableGridLayout is also available with Netscape Navigator. Of the 1.0 layouts, all are available in the JDK and Internet Explorer. Four additional layouts are provided in the sun.awt package: HorizBagLayout, VerticalBagLayout, OrientableFlowLayout, and VariableGridLayout. The java.awt package provides five layout managers: FlowLayout, BorderLayout, GridLayout, CardLayout, and GridBagLayout. Thus, each component must know its space requirements the layout manager uses these to arrange the screen and your Java program never has to worry about platform-dependent positioning. (Java 1.1 also has a getMaximumSize() method the existing layout managers don't take advantage of it.) These methods report the minimum space that a component requires to be displayed correctly and the optimal size at which it looks best. To find out how much space a component needs, a layout manager calls the component's getMinimumSize() and getPreferredSize() methods. It asks each component in the layout how much space it requires, then arranges the components on the screen as best it can, based on the component sizes on the platform in use and the space available, resizing the components as needed. A layout manager takes care of this for you. Even for a simple layout, the code required to discover component sizes and compute absolute positions could be hundreds of lines, particularly if you concern yourself with what happens when the user resizes a window. Layout managers eliminate the need to compute component placement on your own, which would be a losing proposition since the size required for any component depends on the platform on which it is displayed. Every container has a LayoutManager that is responsible for positioning the component objects within it, regardless of the platform or the screen size. We’ll see how we could accomplish this more directly using GridBagLayout shortly.This chapter expands upon the idea of a layout manager, which was mentioned briefly in the previous chapter. Each JPanel’s FlowLayout centers the button in the panel and Now, the JPanel for the CENTER button soaks up the extra space thatĬomes from the BorderLayout. The frame window, which has the BorderLayout manager. In the example, we create a number of panels, put our buttons inside Named for their locations: //file: Border1.java import java.awt.* import .* import javax.swing.* public class Border1 extends JPanel The following application sets a BorderLayout and adds our five buttons again, The name of a position within the BorderLayout. So, we use an overloaded version of the container’s add() method that takes an additional argumentĪs a constraint. To specify both the component and the position at which to add it. When we add a component to a container with a border layout, we need See in the second example, this means that you often want to have BorderLayout manage sets of components in their It squashes or stretches those components to fit its constraints. Because each component isĪssociated with a direction, BorderLayout can manage at most five components The default layout for the content panes of JWindow and JFrame objects. ![]() ![]() Locations, represented by constants in the BorderLayout class: NORTH, SOUTH, EAST, WEST, and CENTER, optionally with ![]() It tries to arrange objects in one of five geographical
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |