Documentation

The Java™ Tutorials
Hide TOC
How to Use Panels如何使用面板
Trail: Creating a GUI With Swing
Lesson: Using Swing Components
Section: How to Use Various Components

How to Use Panels如何使用面板

The JPanel class provides general-purpose containers for lightweight components. JPanel类为轻量级组件提供通用容器。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. 默认情况下,面板不会将颜色添加到除其自身背景之外的任何内容;但是,您可以轻松地为它们添加边框,或者自定义它们的绘制。Details can be found in Performing Custom Painting.有关详细信息,请参阅执行自定义绘制

In many types of look and feel, panels are opaque by default. 在许多类型的外观中,面板默认为不透明。Opaque panels work well as content panes and can help with painting efficiently, as described in Using Top-Level Containers. 不透明面板与内容窗格工作良好,并有助于高效绘制,如使用顶级容器中所述。You can change a panel's transparency by invoking the setOpaque method. 可以通过调用setOpaque方法更改面板的透明度。A transparent panel draws no background, so that any components underneath show through.透明面板不绘制背景,因此下面的任何组件都会显示出来。

An Example举例

The following picture shows a colored version of the Converter application, which is discussed in more detail in Using Models.下图显示了Converter应用程序的彩色版本,将在使用模型中详细讨论。

Colorful Converter

The Converter example uses panels in several ways:Converter示例以多种方式使用面板:

Here is what the Converter application normally looks like.下面是Converter应用程序通常的样子。

Normal Converter

As the Converter example demonstrates, panels are useful for grouping components, simplifying component layout, and putting borders around groups of components. Converter示例所示,面板可用于对组件进行分组、简化组件布局以及在组件组周围放置边框。The rest of this section gives hints on grouping and laying out components. 本节的其余部分给出了组件分组和布局的提示。For information about using borders, see How to Use Borders.有关使用边框的信息,请参阅如何使用边框

Setting the Layout Manager设置布局管理器

Like other containers, a panel uses a layout manager to position and size its components. 与其他容器一样,面板使用布局管理器来定位和调整其组件的大小。By default, a panel's layout manager is an instance of FlowLayout, which places the panel's contents in a row. 默认情况下,面板的布局管理器是FlowLayout的实例,它将面板的内容放置在一行中。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. 通过调用setLayout方法或在创建面板时指定布局管理器,可以轻松地使面板使用任何其他布局管理器。The latter approach is preferable for performance reasons, since it avoids the unnecessary creation of a FlowLayout object.出于性能原因,后一种方法更可取,因为它避免了不必要地创建FlowLayout对象。

Here is an example of how to set the layout manager when creating the panel.以下是创建面板时如何设置布局管理器的示例。

JPanel p = new JPanel(new BorderLayout()); //PREFERRED!

This approach does not work with BoxLayout, since the BoxLayout constructor requires a pre-existing container. 这种方法不适用于BoxLayout,因为BoxLayout构造函数需要预先存在的容器。Here is an example that uses BoxLayout.下面是一个使用BoxLayout的示例。

JPanel p = new JPanel();
p.setLayout(new BoxLayout(p, BoxLayout.PAGE_AXIS));

Adding Components添加组件

When you add components to a panel, you use the add method. 将组件添加到面板时,使用add方法。Exactly which arguments you specify to the add method depend on which layout manager the panel uses. add方法指定的参数取决于面板使用的布局管理器。When the layout manager is FlowLayout, BoxLayout, GridLayout, or SpringLayout, you will typically use the one-argument add method, like this:当布局管理器为FlowLayoutBoxLayoutGridLayoutSpringLayout时,通常使用单参数添加方法,如下所示:

aFlowPanel.add(aComponent);
aFlowPanel.add(anotherComponent);

When the layout manager is BorderLayout, you need to provide an argument specifying the added component's position within the panel. 布局管理器为BorderLayout时,需要提供一个参数,指定添加的组件在面板中的位置。For example:例如:

aBorderPanel.add(aComponent, BorderLayout.CENTER);
aBorderPanel.add(anotherComponent, BorderLayout.PAGE_END);

With GridBagLayout you can use either add method, but you must somehow specify grid bag constraints for each component.对于GridBagLayout,您可以使用add方法,但必须为每个组件指定网格包约束

For information about choosing and using the standard layout managers, see Using Layout Managers.有关选择和使用标准布局管理器的信息,请参阅使用布局管理器

The Panel API面板API

The API in the JPanel class itself is minimal. JPanel类本身的API是最小的。The methods you are most likely to invoke on a JPanel object are those it inherits from its superclasses — JComponent, Container, and Component. 您最可能在JPanel对象上调用的方法是从其超类继承的方法;JComponentContainerComponentThe following tables list the API you are most likely to use, with the exception of methods related to borders and layout hints. 下表列出了最可能使用的API,但与边框布局提示相关的方法除外。For more information about the API that all JComponent objects can use, see The JComponent Class.有关所有JComponent对象都可以使用的API的更多信息,请参阅JComponnt

Creating a 创建JPanel
Constructor构造函数 Purpose目的
JPanel()
JPanel(LayoutManager)
Creates a panel. 创建一个面板。The LayoutManager parameter provides a layout manager for the new panel. LayoutManager参数为新面板提供布局管理器。By default, a panel uses a FlowLayout to lay out its components.默认情况下,面板使用FlowLayout来布局其组件。
Managing a Container's Components管理容器的组件
Method方法 Purpose目的
void add(Component)
void add(Component, int)
void add(Component, Object)
void add(Component, Object, int)
void add(String, Component)
Adds the specified component to the panel. 将指定的组件添加到面板。When present, the int parameter is the index of the component within the container. 如果存在,int参数是容器中组件的索引。By default, the first component added is at index 0, the second is at index 1, and so on. 默认情况下,添加的第一个组件位于索引0,第二个组件位于索引号1,依此类推。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. Object参数依赖于布局管理器,通常向布局管理器提供有关添加组件的定位和其他布局约束的信息。The String parameter is similar to the Object parameter.String参数类似于Object参数。
int getComponentCount() Gets the number of components in this panel.获取此面板中的组件数。
Component getComponent(int)
Component getComponentAt(int, int)
Component getComponentAt(Point)
Component[] getComponents()
Gets the specified component or components. 获取指定的组件。You can get a component based on its index or x, y position.您可以根据组件的索引或x, y位置获取组件。
void remove(Component)
void remove(int)
void removeAll()
Removes the specified component(s).删除指定的组件。
Setting or Getting the Layout Manager设置或获取布局管理器
Method方法 Purpose目的
void setLayout(LayoutManager)
LayoutManager getLayout()
Sets or gets the layout manager for this panel. 设置或获取此面板的布局管理器。The layout manager is responsible for positioning the panel's components within the panel's bounds according to some philosophy.布局管理器负责根据一些原理将面板组件定位在面板边界内。

Examples That Use Panels使用面板的示例

Many examples contained in this lesson use JPanel objects. 本课中包含的许多示例使用JPanel对象。The following table lists a few.下表列出了一些。

Example示例 Where Described描述位置 Notes备注
Converter This section本节 Uses five panels, four of which use BoxLayout and one of which uses GridLayout. 使用五个面板,其中四个使用BoxLayout,一个使用GridLayoutThe panels use borders and, as necessary, size and alignment hints to affect layout.面板使用边框,并根据需要使用大小和对齐提示来影响布局。
ListDemo How to Use Lists如何使用列表 Uses a panel, with its default FlowLayout manager, to center three components in a row.使用带有默认FlowLayout管理器的面板将三个组件居中放置在一行中。
ToolBarDemo How to Use Tool Bars如何使用工具栏 Uses a panel as a content pane. 将面板用作内容窗格。The panel contains three components, laid out by BorderLayout.面板包含三个组件,按BorderLayout布局。
BorderDemo How to Use Borders如何使用边框 Contains many panels that have various kinds of borders. 包含许多具有各种边框的面板。Several panels use BoxLayout.几个面板使用BoxLayout
BoxLayoutDemo How to Use BoxLayout如何使用BoxLayout Illustrates the use of a panel with Swing's BoxLayout manager.演示了使用Swing的BoxLayout管理器的面板。

Previous page: How to Use Menus
Next page: How to Use Password Fields