Documentation

The Java™ Tutorials
Hide TOC
How to Use Separators如何使用分离器
Trail: Creating a GUI With Swing
Lesson: Using Swing Components
Section: How to Use Various Components

How to Use Separators如何使用分离器

The JSeparator class provides a horizontal or vertical dividing line or empty space. JSeparator类提供了水平或垂直分隔线或空白。It's most commonly used in menus and tool bars. 它最常用于菜单和工具栏。In fact, you can use separators without even knowing that a JSeparator class exists, since menus and tool bars provide convenience methods that create and add separators customized for their containers. 事实上,您甚至可以在不知道JSeparator类存在的情况下使用分隔符,因为菜单工具栏提供了创建和添加为容器定制的分隔符的方便方法。Separators are somewhat similar to borders, except that they are genuine components and, as such, are drawn inside a container, rather than around the edges of a particular component.分隔符在某种程度上类似于边界,但它们是真正的组件,因此绘制在容器内,而不是特定组件的边缘周围。

Here is a picture of a menu that has three separators, used to divide the menu into four groups of items:这是一个菜单的图片,它有三个分隔符,用于将菜单分为四组项目:

A menu with 4 parts, as indicated by 3 separators

The code to add the menu items and separators to the menu is extremely simple, boiling down to something like this:将菜单项和分隔符添加到菜单的代码非常简单,归结如下:

menu.add(menuItem1);
menu.add(menuItem2);
menu.add(menuItem3);
menu.addSeparator();
menu.add(rbMenuItem1);
menu.add(rbMenuItem2);
menu.addSeparator();
menu.add(cbMenuItem1);
menu.add(cbMenuItem2);
menu.addSeparator();
menu.add(submenu);

Adding separators to a tool bar is similar. 将分隔符添加到工具栏类似。You can find the full code explained in the how-to sections for menus and tool bars. 您可以在菜单工具栏的“操作”部分中找到完整的代码。If you want more control over separators in menus and tool bars, you can directly use the JSeparator subclasses that implement them: JPopupMenu.Separator and JToolBar.Separator. 如果您想要对菜单和工具栏中的分隔符进行更多控制,可以直接使用实现它们的JSeparator子类:JPopupMenu.SeparatorJToolBar.SeparatorIn particular, JToolBar.Separator has API for specifying the separator's size.特别是,JToolBar.Separator具有用于指定分隔符大小的API。

Using 使用JSeparator

You can use the JSeparator class directly to provide a dividing line in any container. 您可以直接使用JSeparator类在任何容器中提供分隔线。The following picture shows a GUI that has a separator to the right of the button labeled Fire.下图显示了一个GUI,该GUI在标记为Fire的按钮右侧有一个分隔符。

A snapshot of ListDemo

Separators have almost no API and are extremely easy to use as long as you keep one thing in mind: In most implementations, a vertical separator has a preferred height of 0, and a horizontal separator has a preferred width of 0. 分隔符几乎没有API,并且非常容易使用,只要记住一件事:在大多数实现中,垂直分隔符的首选高度为0,水平分隔符的优选宽度为0。This means a separator is not visible unless you either set its preferred size or put it in under the control of a layout manager such as BorderLayout or BoxLayout that stretches it to fill its available display area.这意味着分隔符不可见,除非您设置其首选大小或将其置于布局管理器(如BorderLayoutBoxLayout)的控制下,该布局管理器会拉伸分隔符以填充其可用的显示区域。

The vertical separator does have a bit of width (and the horizontal a bit of height), so you should see some space where the separator is. 垂直分隔符确实有一点宽度(水平分隔符有一点高度),因此您应该看到分隔符所在的空间。However, the actual dividing line isn't drawn unless the width and height are both non-zero.但是,除非宽度和高度都不为零,否则不会绘制实际的分界线。

The following code snippet shows how ListDemo puts together the panel that contains the vertical separator. 下面的代码片段显示了ListDemo如何组合包含垂直分隔符的面板。You can find the full source code for ListDemo in ListDemo.java.您可以在ListDemo.java中找到ListDemo的完整源代码。

JPanel buttonPane = new JPanel();
buttonPane.setLayout(new BoxLayout(buttonPane,
                                   BoxLayout.LINE_AXIS));
buttonPane.add(fireButton);
buttonPane.add(Box.createHorizontalStrut(5));
buttonPane.add(new JSeparator(SwingConstants.VERTICAL));
buttonPane.add(Box.createHorizontalStrut(5));
buttonPane.add(employeeName);
buttonPane.add(hireButton);
buttonPane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));

As the code shows, the buttons, separator, and text field all share a single container — a JPanel instance that uses a left-to-right box layout. 如代码所示,按钮、分隔符和文本字段都共享一个容器;使用从左到右框布局JPanel实例。Thanks to the layout manager (and to the fact that separators have unlimited maximum sizes), the separator is automatically made as tall as its available display area.由于布局管理器(以及分隔符具有无限的最大尺寸),分隔符会自动设置为与其可用显示区域一样高。

In the preceding code, the horizontal struts are invisible components used to provide space around the separator. 在前面的代码中,水平支柱是不可见的组件,用于在分隔器周围提供空间。A 5-pixel empty border provides a cushion around the panel, and also serves to prevent the separator from extending all the way to the component above it and the window's edge below it.5像素的空白边框在面板周围提供了一个缓冲区,还用于防止分隔条一直延伸到其上方的组件和下方的窗口边缘。

Here's a picture of another GUI that uses a separator, this time to put a dividing line between a group of controls and a display area.这是另一个使用分隔符的GUI的图片,这次是在一组控件和一个显示区域之间设置分隔线。

A snapshot of TextInputDemo

You can find the code in the example index. 您可以在示例索引中找到代码。Here is the code that sets up the separator's container:下面是设置分隔符容器的代码:

JPanel panel = new JPanel(new BorderLayout());
...
panel.setBorder(BorderFactory.createEmptyBorder(
                        GAP/2, //top
                        0,     //left
                        GAP/2, //bottom
                        0));   //right
panel.add(new JSeparator(JSeparator.VERTICAL),
          BorderLayout.LINE_START);
panel.add(addressDisplay,
          BorderLayout.CENTER);

As in the last example, the panel uses an empty border so that the separator doesn't extend all the way to the edges of its container. 与上一个示例一样,面板使用空边框,以便分隔符不会一直延伸到其容器的边缘。Placing the separator in the leftmost area of the BorderLayout-controlled container makes the separator as tall as the address-display component that's in the center of the container. 将分隔符放置在BorderLayout控件容器的最左侧区域,使分隔符与容器中心的地址显示组件一样高。See How to Use BorderLayout for details on how border layouts work.有关边框布局如何工作的详细信息,请参阅如何使用边框布局

The Separator API分离器API

The API for using separators is minimal, since they have no contents and don't respond to user input.使用分隔符的API是最小的,因为它们没有内容,不响应用户输入。

Creating and Initializing Separators创建和初始化分隔符
Constructor or Method构造函数或方法 Purpose目的
void addSeparator()
void addSeparator(Dimension)
(in JToolBar)
Append a tool bar separator (which is invisible in most, if not all, look and feels) to the current end of the tool bar. 将工具栏分隔符附加到工具栏的当前端(在大多数(如果不是所有)外观中不可见)。The optional argument specifies the size of the separator. 可选参数指定分隔符的大小。The no-argument version of this method uses a separator with a default size, as determined by the current look and feel.此方法的无参数版本使用默认大小的分隔符,由当前外观确定。
void addSeparator()
void insertSeparator(int)
(in JMenu)
Put a separator in the menu. 在菜单中放置分隔符。The addSeparator method puts the separator at the current end of the menu. addSeparator方法将分隔符放在菜单的当前端。The insertSeparator method inserts the separator into the menu at the specified position.insertSeparator方法在指定位置将分隔符插入菜单。
void addSeparator()
(in JPopupMenu)
Put a separator at the current end of the popup menu.在弹出菜单的当前端放置分隔符。
JSeparator()
JSeparator(int)
Create a separator. 创建分隔符。If you don't specify an argument, the separator is horizontal. 如果不指定参数,则分隔符是水平的。The argument can be either SwingConstants.HORIZONTAL or SwingConstants.VERTICAL.参数可以是SwingConstants.HORIZONTALSwingConstants.VERTICAL
void setOrientation(int)
int getOrientation()
(in JSeparator)
Get or set the separator's orientation, which can be either SwingConstants.HORIZONTAL or SwingConstants.VERTICAL.获取或设置分隔符的方向,可以是SwingConstants.HORIZONTALSwingConstants.VERTICAL
JToolBar.Separator()
JToolBar.Separator(Dimension)
Create a separator for use in a tool bar. 创建用于工具栏的分隔符。The optional argument specifies the separator's size.可选参数指定分隔符的大小。
setSeparatorSize(Dimension)
(in JToolBar.Separator)
Specify the separator's size. 指定分隔符的大小。More specifically, the specified Dimension is used as the separator's minimum, preferred, and maximum sizes.更具体地说,指定的Dimension用作分离器的最小、首选和最大尺寸。
JPopupMenu.Separator() Create a separator for use in a menu.创建用于菜单的分隔符。

Examples that Use Separators使用分隔符的示例

Several of this lesson's examples use separators, usually in menus. 本课的一些示例使用分隔符,通常在菜单中。Here is a list of some of the more interesting examples.下面是一些更有趣的例子。

Example示例 Where Described描述位置 Notes备注
ListDemo This section and 本节和How to Use Lists Uses a vertical separator in a panel controlled by a horizontal box layout.在由水平框布局控制的面板中使用垂直分隔符。
TextInputDemo This section and 本节和How to Use Formatted Text Fields如何使用格式化文本字段 Uses a vertical separator at the left of a panel controlled by a border layout.在由边框布局控制的面板左侧使用垂直分隔符。
MenuDemo This section and 本节和How to Use Menus如何使用菜单 Uses the JMenu method addSeparator to put separators in a menu.使用JMenu方法addSeparator在菜单中放置分隔符。
ToolBarDemo2 How to Use Tool Bars如何使用工具栏 Uses the JToolBar method addSeparator to put space between two kinds of buttons.使用JToolBar方法addSeparator在两种按钮之间留出空间。

If you are programming in JavaFX, see Using JavaFX UI Controls.如果您使用JavaFX编程,请参阅使用JavaFXUI控件


Previous page: How to Use Scroll Panes
Next page: How to Use Sliders