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 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:这是一个菜单的图片,它有三个分隔符,用于将菜单分为四组项目:
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.Separator
和JToolBar.Separator
。In particular, 特别是,JToolBar.Separator
has API for specifying the separator's size.JToolBar.Separator
具有用于指定分隔符大小的API。
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的按钮右侧有一个分隔符。
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.BorderLayout
或BoxLayout
)的控制下,该布局管理器会拉伸分隔符以填充其可用的显示区域。
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的图片,这次是在一组控件和一个显示区域之间设置分隔线。
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 API for using separators is minimal, since they have no contents and don't respond to user input.使用分隔符的API是最小的,因为它们没有内容,不响应用户输入。
void addSeparator()
void addSeparator(Dimension)
(in JToolBar ) |
|
void addSeparator()
void insertSeparator(int)
(in JMenu ) |
addSeparator method puts the separator at the current end of the menu. addSeparator 方法将分隔符放在菜单的当前端。insertSeparator method inserts the separator into the menu at the specified position.insertSeparator 方法在指定位置将分隔符插入菜单。 |
void addSeparator()
(in JPopupMenu ) |
|
JSeparator()
JSeparator(int)
|
SwingConstants.HORIZONTAL or SwingConstants.VERTICAL .SwingConstants.HORIZONTAL 或SwingConstants.VERTICAL 。 |
void setOrientation(int)
int getOrientation()
(in JSeparator ) |
SwingConstants.HORIZONTAL or SwingConstants.VERTICAL .SwingConstants.HORIZONTAL 或SwingConstants.VERTICAL 。 |
JToolBar.Separator() JToolBar.Separator(Dimension) |
|
setSeparatorSize(Dimension)
(in JToolBar.Separator ) |
Dimension is used as the separator's minimum, preferred, and maximum sizes.Dimension 用作分离器的最小、首选和最大尺寸。 |
JPopupMenu.Separator() |
Several of this lesson's examples use separators, usually in menus. 本课的一些示例使用分隔符,通常在菜单中。Here is a list of some of the more interesting examples.下面是一些更有趣的例子。
ListDemo
|
How to Use Lists |
|
TextInputDemo
|
||
MenuDemo
|
JMenu method addSeparator to put separators in a menu.JMenu 方法addSeparator 在菜单中放置分隔符。 | |
ToolBarDemo2
|
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控件。