![]() The layout manager is therefore involved in both sides of the issue. However, since a Container may hold its own components and want to arrange them in its own layout, its preferred size is a function of its layout manager. ![]() The preferred size of a Container object has the same meaning as for any other type of component. Similarly, a particular component might not be able to display itself properly if it is too large (perhaps it has to scale up an image) it can use getMaximumSize() to report the largest size it considers acceptable. The layout manager might show more respect for the button's minimum size and guarantee that it has at least that much space. Now if we give the button a label, the button may need a new minimum size in order to display itself properly. The layout manager might use this size when there are no other constraints, or it might ignore it, depending on its scheme. However, the button's designer can provide a preferred size for a good-looking button. For example, a plain JButton object can normally be changed to any size. These sizes are reported by the getMinimumSize(), getMaximum-Size(), and getPreferredSize() methods of Component, respectively. Calling pack() sets the window's size as small as possible while granting all its components their preferred sizes.Įvery component determines three important pieces of information used by the layout manager in placing and sizing it: a minimum size, a maximum size, and a preferred size. Containers that are a subclass of the Window class ( Frame, JFrame, and JWindow) are automatically validated whenever they are packed or resized. A container is laid out the first time it is displayed and thereafter whenever the container's revalidate() method is called. It does its job by calling the setLocation() or setBounds() methods of the individual child components to arrange them in the container's display area. ![]() The LayoutManager is consulted whenever a container's doLayout() method is called to reorganize the contents. However, you do need to know what the layout manager is going to do with your components as you work with them. You rarely call the layout manager's methods directly, so you don't usually need a reference (a notable exception is CardLayout). This is typical once you have installed a layout manager, it does its work behind the scenes, interacting with the container. Notice that we have called the BorderLayout constructor, but we haven't bothered to save a reference to the layout manager. For example, we can set the layout manager of a Swing container's content pane to a BorderLayout like so: mycontainer.getContentPane( ).setLayout (new BorderLayout( )) You can install a new layout manager at any time with the setLayout() method. A layout manager at workĮvery container has a default layout manager therefore, when you make a new container, it comes with a LayoutManager object of the appropriate type. AWT and Swing come with several standard layout managers that will collectively handle most situations you can make your own layout managers if you have special requirements. The layout manager's job is to fit the components into the available area while maintaining some spatial relationships among them. It positions and sets the size of components within the container's display area according to a particular layout scheme. A layout manager arranges the child components of a container, as shown in Figure 18-1.
0 Comments
Leave a Reply. |