The panel contains three components, laid out by BorderLayout.Ĭontains many panels that have various kinds of borders. Uses a panel, with its default FlowLayout manager, to center three components in a row. The panels use borders and, as necessary, size and alignment hints to affect layout. Uses five panels, four of which use BoxLayout and one of which uses GridLayout. Many examples contained in this lesson use JPanel objects. The layout manager is responsible for positioning the panel's components within the panel's bounds according to some philosophy. Sets or gets the layout manager for this panel. Setting or Getting the Layout Manager Method You can get a component based on its index or x, y position. Gets the specified component or components. Gets the number of components in this panel. The String parameter is similar to the Object parameter. The Object parameter is layout manager dependent and typically provides information to the layout manager regarding positioning and other layout constraints for the added component. By default, the first component added is at index 0, the second is at index 1, and so on. When present, the int parameter is the index of the component within the container. By default, a panel uses a FlowLayout to lay out its components.Īdds the specified component to the panel. The LayoutManager parameter provides a layout manager for the new panel. For more information about the API that all JComponent objects can use, see The JComponent Class.Ĭreates a panel. The following tables list the API you are most likely to use, with the exception of methods related toīorders and layout hints. The methods you are most likely to invoke on a JPanel object are those it inherits from its superclasses Ĭomponent. The API in the JPanel class itself is minimal. With GridBagLayout you can use either add method, but you must somehow specifyįor information about choosing and using the standard layout managers, see Here is an example of how to set the layout manager when creating the panel.ĪBorderPanel.add(aComponent, BorderLayout.CENTER) ĪBorderPanel.add(anotherComponent, BorderLayout.PAGE_END) The latter approach is preferable for performance reasons, since it avoids the unnecessary creation of a FlowLayout object. You can easily make a panel use any other layout manager by invoking the setLayout method or by specifying a layout manager when creating the panel. By default, a panel's layout manager is an instance ofįlowLayout, which places the panel's contents in a row. Like other containers, a panel uses a layout manager to position and size its components. The rest of this section gives hints on grouping and laying out components. Here is what the Converter application normally looks like.Īs the Converter example demonstrates, panels are useful for grouping components, simplifying component layout, and putting borders around groups of components. Each of these JPanel instances uses a top-to-bottom BoxLayout object to lay out its contents. In each ConversionPanel, an instance of an unnamed JPanel subclass colored blue groups two components (a text field and a slider) and restricts their size.Each of these JPanel instances uses a top-to-bottom BoxLayout object (helped by an invisible space-filling component) to lay out the combo box. In each ConversionPanel, a JPanel instance colored magenta is used to ensure the proper size and position of the combo box.Each ConversionPanel panel uses a left-to-right BoxLayout object to lay out its contents. These ConversionPanel panels also have titled borders, which describe their contents and enclose the contents with a line. Two instances of a custom JPanel subclass named ConversionPanel colored cyan are used to contain components and coordinate communication between components.See Using Top-Level Containers for information about content panes. This content pane uses a top-to-bottomīoxLayout to lay out its contents, and an empty border to put 5 pixels of space around them. One JPanel instance colored red in the preceding snapshot serves as a content pane for the application's frame.The Converter example uses panels in several ways: The following picture shows a colored version of the Converter application, which is discussed in more detail in Using Models. A transparent panel draws no background, so that any components underneath show through. You can change a panel's transparency by invoking the setOpaque method. Opaque panels work well as content panes and can help with painting efficiently, as described in In many types of look and feel, panels are opaque by default. By default, panels do not add colors to anything except their own background however, you can easily add borders to them and otherwise customize their painting. JPanel class provides general-purpose containers for lightweight components.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |