The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
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.透明面板不绘制背景,因此下面的任何组件都会显示出来。
The following picture shows a colored version of the 下图显示了Converter application, which is discussed in more detail in Using Models.Converter应用程序的彩色版本,将在使用模型中详细讨论。

The Converter example uses panels in several ways:Converter示例以多种方式使用面板:
JPanel instance colored red in the preceding snapshot serves as a content pane for the application's frame. JPanel实例;在前面的快照中为红色用作应用程序框架的内容窗格。BoxLayout to lay out its contents, and an empty border to put 5 pixels of space around them. BoxLayout来布局其内容,并使用空边框在其周围放置5像素的空间。JPanel subclass named ConversionPanel colored cyan are used to contain components and coordinate communication between components. ConversionPanel的自定义JPanel子类的两个实例;彩色青色;用于包含组件并协调组件之间的通信。ConversionPanel panels also have titled borders, which describe their contents and enclose the contents with a line. Each ConversionPanel panel uses a left-to-right BoxLayout object to lay out its contents.ConversionPanel面板还具有标题边框,用于描述其内容,并用一行包围内容。每个ConversionPanel面板都使用从左到右的BoxLayout对象来布局其内容。ConversionPanel, a JPanel instance colored magenta is used to ensure the proper size and position of the combo box. ConversionPanel中;一个JPanel实例彩色品红色用于确保组合框的大小和位置正确。JPanel instances uses a top-to-bottom BoxLayout object (helped by an invisible space-filling component) to lay out the combo box.JPanel实例都使用一个自上而下的BoxLayout对象(由一个不可见的空间填充组件帮助)来布局组合框。ConversionPanel, an instance of an unnamed JPanel subclass colored blue groups two components (a text field and a slider) and restricts their size. ConversionPanel中,一个未命名JPanel子类的实例蓝色将两个组件(文本字段和滑块)分组并限制其大小。JPanel instances uses a top-to-bottom BoxLayout object to lay out its contents.JPanel实例都使用一个自上而下的BoxLayout对象来布局其内容。Here is what the 下面是Converter application normally looks like.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.有关使用边框的信息,请参阅如何使用边框。
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));
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:FlowLayout、BoxLayout,GridLayout或SpringLayout时,通常使用单参数添加方法,如下所示:
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 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对象上调用的方法是从其超类继承的方法;JComponent、Container和Component。The 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类。
JPanelJPanel()JPanel(LayoutManager) |
LayoutManager parameter provides a layout manager for the new panel. LayoutManager参数为新面板提供布局管理器。FlowLayout to lay out its components.FlowLayout来布局其组件。 |
void add(Component)void add(Component, int)void add(Component, Object)void add(Component, Object, int)void add(String, Component) |
int parameter is the index of the component within the container. int参数是容器中组件的索引。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参数依赖于布局管理器,通常向布局管理器提供有关添加组件的定位和其他布局约束的信息。String parameter is similar to the Object parameter.String参数类似于Object参数。 |
int getComponentCount() |
|
Component getComponent(int)Component getComponentAt(int, int)Component getComponentAt(Point)Component[] getComponents() |
|
void remove(Component)void remove(int)void removeAll() |
void setLayout(LayoutManager)LayoutManager getLayout() |
Many examples contained in this lesson use 本课中包含的许多示例使用JPanel objects. JPanel对象。The following table lists a few.下表列出了一些。
Converter |
BoxLayout and one of which uses GridLayout. BoxLayout,一个使用GridLayout。 | |
ListDemo |
FlowLayout manager, to center three components in a row.FlowLayout管理器的面板将三个组件居中放置在一行中。 | |
ToolBarDemo |
BorderLayout.BorderLayout布局。 | |
BorderDemo |
BoxLayout.BoxLayout。 | |
BoxLayoutDemo |
BoxLayout manager.BoxLayout管理器的面板。 |