Documentation

The Java™ Tutorials
Hide TOC
How to Use Split Panes如何使用拆分窗格
Trail: Creating a GUI With Swing
Lesson: Using Swing Components
Section: How to Use Various Components

How to Use Split Panes如何使用拆分窗格

A JSplitPane displays two components, either side by side or one on top of the other. JSplitPane显示两个组件,要么并排显示,要么一个在另一个之上。By dragging the divider that appears between the components, the user can specify how much of the split pane's total area goes to each component. 通过拖动组件之间出现的分隔线,用户可以指定拆分窗格的总面积中有多少分配给每个组件。You can divide screen space among three or more components by putting split panes inside of split panes, as described in Nesting Split Panes.通过将拆分窗格放置在拆分窗格中,可以在三个或更多组件之间划分屏幕空间,如嵌套拆分窗格中所述。

Instead of adding the components of interest directly to a split pane, you often put each component into a scroll pane. 您通常将每个组件放在滚动窗格中,而不是将感兴趣的组件直接添加到拆分窗格中。You then put the scroll panes into the split pane. 然后将滚动窗格放入拆分窗格。This allows the user to view any part of a component of interest, without requiring the component to take up a lot of screen space or adapt to displaying itself in varying amounts of screen space.这允许用户查看感兴趣组件的任何部分,而无需组件占用大量屏幕空间或适应在不同数量的屏幕空间中显示自身。

Here's a picture of an application that uses a split pane to display a list and an image side by side:下面是一个应用程序的图片,它使用拆分窗格并排显示列表和图像:

A snapshot of SplitPaneDemo

Try this: 
  1. Click the Launch button to run the SplitPaneDemo using Java™ Web Start (download JDK 7 or later). 单击启动按钮,使用Java™ Web StartJava™Web启动运行SplitPaneDemo(下载JDK 7或更高版本)。Alternatively, to compile and run the example yourself, consult the example index.或者,要自己编译和运行示例,请参考示例索引Launches the SplitPaneDemo example
  2. Drag the dimpled line that divides the list and the image to the left or right. 向左或向右拖动分隔列表和图像的带酒窝线。Try to drag the divider all the way to the window's edge.尝试将分隔线一直拖动到窗口边缘。
  3. Click the tiny arrows on the divider to hide/expand the left or right component.单击分隔器上的小箭头以隐藏/展开左侧或右侧组件。

Below is the code from SplitPaneDemo that creates and sets up the split pane.下面是SplitPaneDemo中创建和设置拆分窗格的代码。

//Create a split pane with the two scroll panes in it.
splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
                           listScrollPane, pictureScrollPane);
splitPane.setOneTouchExpandable(true);
splitPane.setDividerLocation(150);

//Provide minimum sizes for the two components in the split pane
Dimension minimumSize = new Dimension(100, 50);
listScrollPane.setMinimumSize(minimumSize);
pictureScrollPane.setMinimumSize(minimumSize);

The constructor used by this example takes three arguments. 本例使用的构造函数有三个参数。The first indicates the split direction. 第一个指示分裂方向。The other arguments are the two components to put in the split pane. 其他参数是要放在拆分窗格中的两个组件。Refer to Setting the Components in a Split Pane for information about JSplitPane methods that set the components dynamically.有关动态设置组件的JSplitPane方法的信息,请参阅在拆分窗格中设置组件

The split pane in this example is split horizontally — the two components appear side by side — as specified by the JSplitPane.HORIZONTAL_SPLIT argument to the constructor. 该示例中的分割窗格被水平分割;这两个组件并排出现;如构造函数的JSplitPane.HORIZONTAL_SPLIT参数所指定。Split pane provides one other option, specified with JSplitPane.VERTICAL_SPLIT, that places one component above the other. 拆分窗格提供了另一个选项,该选项由JSplitPane.VERTICAL_SPLIT指定,用于将一个组件置于另一个组件之上。You can change the split direction after the split pane has been created with the setOrientation method.使用setOrientation方法创建拆分窗格后,可以更改拆分方向。

Two small arrows appear at the top of the divider in the example's split pane. 在示例的拆分窗格中,分隔符的顶部出现两个小箭头。These arrows let the user collapse (and then expand) either of the components with a single click. 通过这些箭头,用户只需单击一次,即可折叠(然后展开)任一组件。The current look and feel determines whether these controls appear by default. 当前外观决定这些控件是否默认显示。In the Java look and feel, they are turned off by default. (Note that not all look and feels support this.) 在Java外观中,它们在默认情况下是关闭的。(请注意,并非所有外观和感觉都支持这一点。)The example turned them on using the setOneTouchExpandable method.示例使用setOneTouchExpandable方法打开了它们。

The range of a split pane's divider is determined in part by the minimum sizes of the components within the split pane. 拆分窗格的分隔线范围部分由拆分窗格中组件的最小尺寸确定。See Positioning the Divider and Restricting its Range for details.有关详细信息,请参阅定位分隔器并限制其范围

The rest of this section covers these topics:本节其余部分涵盖以下主题:

Setting the Components in a Split Pane在拆分窗格中设置组件

A program can set a split pane's two components dynamically with these four methods:程序可以使用以下四种方法动态设置拆分窗格的两个组件:

You can use any of these methods at any time regardless of the split pane's current split direction. 无论拆分窗格的当前拆分方向如何,您都可以随时使用这些方法。Calls to setLeftComponent and setTopComponent are equivalent and set the specified component in the top or left position, depending on the split pane's current split orientation. setLeftComponentsetTopComponent的调用是等效的,并将指定组件设置在顶部或左侧位置,具体取决于拆分窗格的当前拆分方向。Similarly, calls to setRightComponent and setBottomComponent are equivalent. 类似地,对setRightComponentsetBottomComponent的调用是等效的。These methods replace whatever component is already in that position with the new one.这些方法用新的组件替换已经处于该位置的组件。

Like other containers, JSplitPane supports the add method. 与其他容器一样,JSplitPane支持add方法。Split pane puts the first component added in the left or top position. 拆分窗格将添加的第一个组件置于左侧或顶部位置。The danger of using add is that you can inadvertently call it too many times, in which case the split pane's layout manager will throw a rather esoteric-looking exception. 使用add的危险在于,您可能会不经意地调用它太多次,在这种情况下,拆分窗格的布局管理器将抛出一个看起来相当神秘的异常。If you are using the add method and a split pane is already populated, you first need to remove the existing components with remove.如果使用add方法,并且已填充拆分窗格,则首先需要使用remove删除现有组件。

If you put only one component in a split pane, then the divider will be stuck at the right side or the bottom of the split pane, depending on its split direction.如果在拆分窗格中仅放置一个组件,则分隔符将被固定在拆分窗格的右侧或底部,具体取决于其拆分方向。

Positioning the Divider and Restricting Its Range定位分隔器并限制其范围

To make your split pane work well, you often need to set the minimum sizes of components in the split pane, as well as the preferred size of either the split pane or its contained components. 要使拆分窗格正常工作,通常需要设置拆分窗格中组件的最小大小,以及拆分窗格或其包含组件的首选大小。Choosing which sizes you should set is an art that requires understanding how a split pane's preferred size and divider location are determined. Before we get into details, let's take another look at SplitPaneDemo. 选择应设置的大小是一门艺术,需要了解如何确定拆分窗格的首选大小和分隔位置。在我们进入细节之前,让我们再看看SplitPaneDemo。Or, if you're in a hurry, you can skip to the list of rules.或者,如果您赶时间,可以跳到规则列表


Try this: 
  1. Click the Launch button to run the SplitPaneDemo using Java™ Web Start (download JDK 7 or later). 单击启动按钮,使用Java™Web启动运行SplitPaneDemo(下载JDK 7或更高版本)。Alternatively, to compile and run the example yourself, consult the example index.或者,要自己编译和运行示例,请参考示例索引Launches the SplitPaneDemo example

    Because the size of the demo's frame is set using the pack method, the split pane is at its preferred size, which SplitPaneDemo happens to set explicitly. 因为演示框架的大小是使用pack方法设置的,所以拆分窗格的大小是其首选大小,而SplitPaneDemo恰好是显式设置的。The divider is automatically placed so that the left component is at its preferred width and all remaining space goes to the right component.分隔器将自动放置,以使左侧组件处于其首选宽度,所有剩余空间都将流向右侧组件。
  2. Make the window wider.把窗户开大些。
    The divider stays where it is, and the extra space goes to the component at the right.分隔符保持在原来的位置,多余的空间将分配给右边的组件。
  3. Make the window noticeably narrower than when it first came up — perhaps twice as wide as the left component.使窗口比第一次出现时明显变窄—可能是左组件的两倍宽。
    Again, the left component's size and the divider position stay the same. 同样,左侧组件的大小和分隔器位置保持不变。Only the size of the right component changes.只有正确组件的大小发生变化。
  4. Make the window as narrow as possible.把窗户尽量窄些。
    Assuming the window uses the Java look and feel-provided decorations, you cannot size the window smaller than the split pane's minimum size, which is determined by the minimum size of the components contained by the split pane. 假设窗口使用Java外观提供的装饰,则窗口的大小不能小于拆分窗格的最小大小,该最小大小由拆分窗格包含的组件的最小大小决定。SplitPaneDemo sets the minimum size of these contained components explicitly.SplitPaneDemo显式设置这些包含组件的最小大小。
  5. Make the window wider, and then drag the divider as far as it will go to the right.使窗口更宽,然后将分隔线拖到最右侧。
    The divider goes only as far as the right component's minimum size allows. 分隔器仅在正确组件的最小尺寸允许的范围内。If you drag the divider to the left, you'll see that it also respects the left component's minimum size.如果将分隔线拖动到左侧,您将看到它也符合左侧组件的最小尺寸。

Now that you've seen the default behavior of split panes, we can tell you what's happening behind the scenes and how you can affect it. 现在您已经看到了拆分窗格的默认行为,我们可以告诉您幕后发生了什么,以及您如何影响它。In this discussion, when we refer to a component's preferred or minimum size, we often mean the preferred or minimum width of the component if the split pane is horizontal, or its preferred or minimum height if the split pane is vertical.在本讨论中,当我们提到组件的首选或最小尺寸时,我们通常指的是组件的首选宽度或最小宽度(如果拆分窗格是水平的),或其首选或最小高度(如果拆分面板是垂直的)。

By default, a split pane's preferred size and divider location are initialized so that the two components in the split pane are at their preferred sizes. 默认情况下,将初始化拆分窗格的首选大小和分隔符位置,以便拆分窗格中的两个组件处于其首选大小。If the split pane isn't displayed at this preferred size and the program hasn't set the divider's location explicitly, then the initial position of the divider (and thus the sizes of the two components) depends on a split pane property called the resize weight. 如果拆分窗格未按此首选大小显示,并且程序未明确设置分隔符的位置,则分隔符的初始位置(以及两个组件的大小)取决于称为“调整大小权重”的拆分窗格属性。If the split pane is initially at its preferred size or bigger, then the contained components start out at their preferred sizes, before adjusting for the resize weight. 如果拆分窗格最初为其首选大小或更大,则包含的组件将在调整大小权重之前以其首选大小开始。If the split pane is initially too small to display both components at their preferred sizes, then they start out at their minimum sizes, before adjusting for the resize weight.如果拆分窗格最初太小,无法以其首选尺寸显示两个组件,则在调整大小权重之前,它们将以最小尺寸开始。

A split pane's resize weight has a value between 0.0 and 1.0 and determines how space is distributed between the two contained components when the split pane's size is set — whether programmatically or by the user resizing the split pane (enlarging its containing window, for example). 分割窗格的调整大小权重的值介于0.0和1.0之间,并确定当分割窗格的大小设置为;无论是通过编程方式还是通过用户调整拆分窗格的大小(例如,放大其包含的窗口)。The resize weight of a split pane is 0.0 by default, indicating that the left or top component's size is fixed, and the right or bottom component adjusts its size to fit the remaining space. 默认情况下,拆分窗格的调整大小权重为0.0,表示左侧或顶部组件的大小是固定的,右侧或底部组件调整其大小以适应剩余空间。Setting the resize weight to 0.5 splits any extra or missing space evenly between the two components. 将“调整大小权重”设置为0.5将在两个组件之间均匀分割任何多余或缺失的空间。Setting the resize weight to 1.0 makes the right or bottom component's size remain fixed. 将“调整大小权重”设置为1.0将使右侧或底部组件的大小保持固定。The resize weight has no effect, however, when the user drags the divider.但是,当用户拖动分隔符时,调整大小权重没有影响。

The user can drag the divider to any position as long as neither contained component goes below its minimum size. 用户可以将分隔器拖动到任何位置,只要包含的组件都不低于其最小尺寸。If the divider has one-touch buttons, the user can use them to make the divider move completely to one side or the other — no matter what the minimum sizes of the components are.如果分隔器具有单触按钮,则用户可以使用它们使分隔器完全移动到一侧或另一侧;无论组件的最小尺寸是多少。

Now that you know the factors that affect a split pane's size and divider location, here are some rules for making them work well:现在您已经知道了影响拆分窗格大小和分隔符位置的因素,下面是一些使其正常工作的规则:

The following snapshot shows an example named SplitPaneDividerDemo that demonstrates split pane component sizes and divider placement.下面的快照显示了名为SplitPaneDividerDemo的示例,该示例演示了拆分窗格组件大小和分隔器放置。

A snapshot of SplitPaneDividerDemo

Like SplitPaneDemo, SplitPaneDividerDemo features a horizontal split pane with one-touch buttons. 与SplitPaneDemo类似,SplitPanedViderDemo具有带有一键式按钮的水平拆分窗格。SplitPaneDividerDemo has the following additional features:SplitPaneDividerDemo具有以下附加功能:


Try this: 
  1. Click the Launch button to run the SplitPaneDividerDemo using Java™ Web Start (download JDK 7 or later). 单击启动按钮,使用Java™Web启动运行SplitPaneDividerDemo(下载JDK 7或更高版本)。Alternatively, to compile and run the example yourself, consult the example index.或者,要自己编译和运行示例,请参考示例索引Launches the SplitPaneDividerDemo example

    Because the size of the demo's frame is set using the pack method, the split pane is at its preferred size, which by default is just big enough for the SizeDisplayers to be at their preferred sizes. 因为演示框架的大小是使用pack方法设置的,所以拆分窗格的大小是其首选大小,默认情况下,该大小刚好足以使SizeDisplayer处于其首选大小。The preferred size of each SizeDisplayer is indicated by a red rectangle. 每个SizeDisplayer的首选大小由红色矩形表示。The divider is automatically placed so that both components are at their preferred widths.自动放置分隔器,以便两个组件都处于其首选宽度。
  2. Make the window wider.把窗户开大些。
    Because the split pane's resize weight is 0.5, the extra space is divided evenly between the left and right components. 由于拆分窗格的调整大小权重为0.5,因此额外的空间在左组件和右组件之间平均分配。The divider moves accordingly.分隔器相应地移动。
  3. Make the window as narrow as possible.把窗户尽量窄些。
    Assuming the window uses the Java look and feel-provided decorations, it will not let you size the window smaller than the split pane's minimum size, which is determined by the minimum size of the SizeDisplayers it contains. 假设窗口使用Java外观提供的装饰,它将不允许您将窗口的大小设置为小于拆分窗格的最小大小,该最小大小由它包含的SizeDisplayer的最小大小决定。The minimum size of each SizeDisplayer is indicated by a bright blue rectangle.每个SizeDisplayer的最小大小由亮蓝色矩形表示。
  4. Make the window a bit wider, and then drag the divider as far as it will go to the right.使窗口稍宽一点,然后将分隔线拖到最右侧。
    The divider goes only as far as the right component's minimum size allows.分隔器仅在正确组件的最小尺寸允许的范围内。
  5. After making sure the split pane is smaller than its preferred size, click the Reset button.确保拆分窗格小于其首选大小后,单击重置按钮。
    Note that the two SizeDisplayers are displayed at the different sizes, even though when the application came up they had equal sizes. 请注意,两个SizeDisplayer以不同的大小显示,即使在应用程序启动时它们的大小相同。The reason is that although their preferred sizes are equal, their minimum sizes are not. 原因是,尽管它们的首选尺寸相同,但它们的最小尺寸却不同。Because the split pane cannot display them at their preferred sizes or larger, it lays them out using their minimum sizes. 由于拆分窗格无法以其首选大小或更大的大小显示它们,因此它使用它们的最小大小进行布局。The leftover space is divided equally between the components, since the split pane's resize weight is 0.5.剩余空间在组件之间平均分配,因为拆分窗格的调整大小权重为0.5。
  6. Widen the split pane so that it is large enough for both SizeDisplayers to be shown at their preferred sizes, and then click the Reset button.加宽拆分窗格,使其足够大,以便两个SizeDisplayer都以其首选大小显示,然后单击重置按钮。
    The divider is placed in the middle again, so that both components are the same size.分隔器再次放置在中间,以便两个组件的大小相同。

Here is the code that creates the GUI for SplitPaneDividerDemo:下面是为SplitPaneDividerDemo创建GUI的代码:

public class SplitPaneDividerDemo extends JPanel ... {

    private JSplitPane splitPane;
   
    public SplitPaneDividerDemo() {
        super(new BorderLayout());

        Font font = new Font("Serif", Font.ITALIC, 24);

        ImageIcon icon = createImageIcon("images/Cat.gif");
        SizeDisplayer sd1 = new SizeDisplayer("left", icon);
        sd1.setMinimumSize(new Dimension(30,30));
        sd1.setFont(font);
        
        icon = createImageIcon("images/Dog.gif");
        SizeDisplayer sd2 = new SizeDisplayer("right", icon);
        sd2.setMinimumSize(new Dimension(60,60));
        sd2.setFont(font);
        
        splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
                                   sd1, sd2);
        splitPane.setResizeWeight(0.5);
        splitPane.setOneTouchExpandable(true);
        splitPane.setContinuousLayout(true);

        add(splitPane, BorderLayout.CENTER);
        add(createControlPanel(), BorderLayout.PAGE_END);
    }
    ...
}

The code is fairly self explanatory, except perhaps for the call to setContinuousLayout. 除了调用setContinuousLayout之外,代码是相当自解释的。Setting the continuousLayout property to true makes the split pane's contents be painted continuously while the user is moving the divider. 如果将continuousLayout属性设置为true,则在用户移动分隔符时,可以连续绘制分割窗格的内容。Continuous layout is not on, by default, because it can have a negative performance impact. 默认情况下,连续布局未启用,因为它可能会对性能产生负面影响。However, it makes sense to use it in this demo, when having the split pane's components as up-to-date as possible can improve the user experience.然而,在本演示中使用它是有意义的,因为尽可能更新拆分窗格的组件可以改善用户体验。

Nesting Split Panes嵌套拆分窗格

Here's a picture of a program that achieves a three-way split by nesting one split pane inside of another:下面是一个程序的图片,它通过将一个拆分窗格嵌套在另一个窗格中来实现三向拆分:

A snapshot of SplitPaneDemo2

If the top portion of the split pane looks familiar to you, it is because the program puts the split pane created by SplitPaneDemo inside a second split pane. 如果拆分窗格的顶部看起来很熟悉,这是因为程序将SplitPaneDemo创建的拆分窗格放在第二个拆分窗格中。A simple JLabel is the other component in the second split pane. 简单的JLabel是第二个拆分窗格中的另一个组件。This is not the most practical use of a nested split pane, but it gets the point across.这不是嵌套拆分窗格的最实际的用法,但它能让人明白这一点。

Launches the SplitPaneDemo2 example

Here's the interesting part of the code, which you can find in SplitPaneDemo2.java:下面是代码的有趣部分,您可以在SplitPaneDemo2.java中找到:

//Create an instance of SplitPaneDemo
SplitPaneDemo splitPaneDemo = new SplitPaneDemo();
JSplitPane top = splitPaneDemo.getSplitPane();

...
//Create a regular old label
label = new JLabel("Click on an image name in the list.",
                   JLabel.CENTER);

//Create a split pane and put "top" (a split pane)
//and JLabel instance in it.
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
                                      top, label);

Refer to Solving Common Component Problems for information about fixing a border problem that can appear when nesting split panes.有关修复嵌套拆分窗格时可能出现的边界问题的信息,请参阅解决常见组件问题

The Split Pane API拆分窗格API

The following tables list the commonly used JSplitPane constructors and methods. 下表列出了常用的JSplitPane构造函数和方法。Other methods you are most likely to invoke on a JSplitPane object are those such as setPreferredSize that its superclasses provide. 您最可能在JSplitPane对象上调用的其他方法是其超类提供的setPreferredSizeSee The JComponent API for tables of commonly used inherited methods.有关常用继承方法的表,请参阅JComponent API

The API for using lists falls into these categories:使用列表的API分为以下几类:

Setting Up the Split Pane设置拆分窗格
Method or Constructor方法或构造函数 Purpose目的
JSplitPane()
JSplitPane(int)
JSplitPane(int, boolean)
JSplitPane(int, Component, Component)
JSplitPane(int, boolean, Component, Component)
Create a split pane. 创建拆分窗格。When present, the int parameter indicates the split pane's orientation, either HORIZONTAL_SPLIT (the default) or VERTICAL_SPLIT. 如果存在,int参数表示拆分窗格的方向,即HORIZONTAL_SPLIT(默认)或VERTICAL_SPLITThe boolean parameter, when present, sets whether the components continually repaint as the user drags the split pane. boolean参数(如果存在)设置当用户拖动拆分窗格时组件是否持续重新绘制。If left unspecified, this option (called continuous layout) is turned off. 如果未指定,则此选项(称为连续布局)将关闭。The Component parameters set the initial left and right, or top and bottom components, respectively.Component参数分别设置初始的左组件和右组件,或顶部组件和底部组件。
void setOrientation(int)
int getOrientation()
Set or get the split pane's orientation. 设置或获取拆分窗格的方向。Use either HORIZONTAL_SPLIT or VERTICAL_SPLIT defined in JSplitPane. 使用JSplitPane中定义的HORIZONTAL_SPLITVERTICAL_SPLITIf left unspecified, the split pane will be horizontally split.如果未指定,则拆分窗格将水平拆分。
void setDividerSize(int)
int getDividerSize()
Set or get the size of the divider in pixels.设置或获取分隔符的大小(以像素为单位)。
void setContinuousLayout(boolean)
boolean isContinuousLayout()
Set or get whether the split pane's components are continually laid out and painted while the user is dragging the divider. 设置或获取在用户拖动分隔线时是否连续布局和绘制拆分窗格的组件。By default, continuous layout is turned off.默认情况下,连续布局处于关闭状态。
void setOneTouchExpandable(boolean)
boolean isOneTouchExpandable()
Set or get whether the split pane displays a control on the divider to expand/collapse the divider. 设置或获取拆分窗格是否显示分隔符上的控件以展开/折叠分隔符。The default depends on the look and feel. 默认值取决于外观。In the Java look and feel, it is off by default.在Java外观中,默认情况下它处于关闭状态。
Managing the Split Pane's Contents管理拆分窗格的内容
Method方法 Purpose目的
void setTopComponent(Component)
void setBottomComponent(Component)
void setLeftComponent(Component)
void setRightComponent(Component)
Component getTopComponent()
Component getBottomComponent()
Component getLeftComponent()
Component getRightComponent()
Set or get the indicated component. 设置或获取指定的组件。Each method works regardless of the split pane's orientation. 无论拆分窗格的方向如何,每个方法都可以工作。Top and left are equivalent, and bottom and right are equivalent.顶部和左侧是等效的,底部和右侧是等效的。
void remove(Component)
void removeAll()
Remove the indicated component(s) from the split pane.从拆分窗格中删除指定的组件。
void add(Component) Add the component to the split pane. 将组件添加到拆分窗格。You can add only two components to a split pane. 只能向拆分窗格添加两个组件。The first component added is the top/left component. 添加的第一个组件是顶部/左侧组件。The second component added is the bottom/right component. 添加的第二个组件是底部/右侧组件。Any attempt to add more components results in an exception.任何添加更多组件的尝试都会导致异常。
Positioning the Divider定位分隔器
Method方法 Purpose目的
void setDividerLocation(double)
void setDividerLocation(int)
int getDividerLocation()
Set or get the current divider location. 设置或获取当前分频器位置。When setting the divider location, you can specify the new location as a percentage (double) or a pixel location (int).设置除法器位置时,可以将新位置指定为百分比(double)或像素位置(int)。
void resetToPreferredSizes() Move the divider such that both components are at their preferred sizes. 移动分隔器,使两个组件都处于其首选尺寸。This is how a split pane divides itself at startup, unless specified otherwise.除非另有规定,否则拆分窗格在启动时是这样划分的。
void setLastDividerLocation(int)
int getLastDividerLocation()
Set or get the previous position of the divider.设置或获取分隔器的先前位置。
int getMaximumDividerLocation()
int getMinimumDividerLocation()
Get the minimum and maximum locations for the divider. 获取除法器的最小和最大位置。These are set implicitly by setting the minimum sizes of the split pane's two components.通过设置拆分窗格的两个组件的最小大小来隐式设置这些组件。
void setResizeWeight(float)
float getResizeWeight()
Set or get the resize weight for the split pane, a value between 0.0 (the default) and 1.0. 设置或获取拆分窗格的调整大小权重,值介于0.0(默认值)和1.0之间。See Positioning the Divider and Restricting Its Range for an explanation of and examples of using the resize weight.有关使用调整大小权重的说明和示例,请参阅定位分隔器并限制其范围

Examples that Use Split Panes使用拆分窗格的示例

This table shows some examples that use JSplitPane and where those examples are described.下表显示了一些使用JSplitPane的示例,以及这些示例的描述位置。

Example示例 Where Described描述位置 Notes备注
SplitPaneDemo This page and 本页和How to Use Lists如何使用列表 Shows a split pane with a horizontal split.显示水平拆分的拆分窗格。
SplitPaneDividerDemo This page本页 Demonstrates how component size information and resize weight are used to position the divider.演示如何使用组件大小信息和调整大小权重来定位分隔线。
SplitPaneDemo2 This page本页 Puts a split pane within a split pane to create a three-way split.在拆分窗格中放置拆分窗格以创建三向拆分。
TreeDemo How to Use Trees如何使用树木 Uses a split pane with a vertical split to separate a tree (in a scroll pane) from an editor pane (in a scroll pane). 使用带有垂直拆分的拆分窗格将树(滚动窗格中)与编辑器窗格(滚动窗格)分隔开。Does not use the one-touch expandable feature.不使用单触式可扩展功能。
TextComponentDemo Text Component Features文本组件功能 Uses a split pane with a vertical split to separate a text pane and a text area, both in scroll panes.使用带有垂直拆分的拆分窗格来分隔文本窗格和文本区域,两者都在滚动窗格中。
TextSamplerDemo Text Component Features文本组件功能 Uses a split pane with a vertical split and resize weight of 0.5 to separate a text pane and an editor pane, both in scroll panes. 使用垂直拆分和调整大小权重为0.5的拆分窗格来分隔滚动窗格中的文本窗格和编辑器窗格。The split pane is in the right half of a container that has a fairly complicated layout. 拆分窗格位于布局相当复杂的容器的右半部分。Layout managers such as GridLayout and BorderLayout are used, along with the split pane's resize weight, to ensure that the components in scroll panes share all extra space.使用布局管理器(如GridLayoutBorderLayout)以及拆分窗格的调整大小权重,以确保滚动窗格中的组件共享所有额外空间。
ListSelectionDemo How to Write a List Selection Listener如何编写列表选择侦听器 Uses a split pane with a vertical split to separate an upper pane, containing a list and a table (both in scroll panes), from a lower pane that contains a combo box above a scroll pane. 使用带有垂直拆分的拆分窗格将包含列表和表格(均在滚动窗格中)的上部窗格与包含滚动窗格上方组合框的下部窗格分隔开。The lower pane uses a border layout to keep the combo box small and the scroll pane greedy for space.下窗格使用边框布局来保持组合框较小,滚动窗格贪婪地占用空间。

Previous page: How to Use Spinners
Next page: How to Use Tabbed Panes