15

我有一个 JSplitPane,它在显示时应该将窗格拆分 50%。

现在,在给 setDividerLocation 一个参数 0.5(如建议的那样)时,Java 似乎将其视为正常数字而不是百分比。与中一样,分隔线几乎位于左侧窗格的开头(窗格垂直拆分),而不是转到窗格的中间。有什么解决办法吗?

4

10 回答 10

28

我错过了什么吗?这个问题似乎有很多相当复杂的答案......但我认为一个简单的 setResizeWeight(0.5) 可以解决这个问题......它在SplitPane 教程中有所描述

于 2011-08-14T19:34:03.417 回答
17

setDividerLocation(double) 方法仅适用于“已实现”的框架,这意味着在您打包或使框架可见之后。

可以随时使用 setDividerLocation( int ) 方法。

于 2009-12-10T07:08:11.360 回答
8

当拆分窗格可见时,您只能将拆分窗格分隔线的位置设置为百分比。您可以点击拆分窗格所有者的事件以确定何时可以设置分隔线的位置。例如:

public class MyFrame extends JFrame {

  public MyFrame() {

    final JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
    // ... set up the split pane, and add to the frame ...

    // Listen for the frame's "shown" event.

    addComponentListener(new ComponentAdapter() {

      @Override
      public void componentShown(ComponentEvent componentEvent) {

        // Set the divider location to 20%.
        // This works because we know the pane is visible.

        splitPane.setDividerLocation(.2);

        // Stop listening for "shown" events.

        removeComponentListener(this);
      }

    });

    pack();
  }
}
于 2012-12-28T19:51:27.940 回答
3

这解决了它:

public class JSplitPane3 extends JSplitPane {
    private boolean hasProportionalLocation = false;
    private double proportionalLocation = 0.5;
    private boolean isPainted = false;

    public void setDividerLocation(double proportionalLocation) {
        if (!isPainted) {
            hasProportionalLocation = true;
            this.proportionalLocation = proportionalLocation;
        } else {
            super.setDividerLocation(proportionalLocation);
        }
    }

    public void paint(Graphics g) {
        super.paint(g);
        if (!isPainted) {
            if (hasProportionalLocation) {
                super.setDividerLocation(proportionalLocation);
            }
            isPainted = true;
        }
    }

}
于 2010-06-02T10:20:33.227 回答
2

如果您对每次调整窗格大小时分隔线移动到中间感到满意,则可以添加一个 ComponentListener 并让其 componentResized 方法调用 setDividerLocation(0.5)。

于 2009-12-10T07:22:22.893 回答
2

利用setResizeWeight(double);

于 2012-05-09T17:24:32.530 回答
2

我发现setResizeWeightsetDividerLocation之后调用的组合提供了人们期望的行为:

_splitPane.setResizeWeight(0.5);

SwingUtilities.invokeLater(new Runnable() {
    @Override
    public void run() {
        _splitPane.setDividerLocation(0.5);
    }
});
于 2015-08-17T19:22:23.647 回答
1

This works for me, based on Dave Rays link.

/**
 * Set JSplitPane proportional divider location
 * 
 * @param jsplitpane JSplitPane to set
 * @param proportionalLocation double <0.0; 1.0>
 */
public static void setJSplitPaneDividerLocation(final JSplitPane jsplitpane, final double proportionalLocation)
{
    if (jsplitpane.isShowing())
    {
        if (jsplitpane.getWidth() > 0 && jsplitpane.getHeight() > 0)
        {
            jsplitpane.setDividerLocation(proportionalLocation);
        }
        else
        {
            jsplitpane.addComponentListener(new ComponentAdapter()
            {
                @Override
                public void componentResized(ComponentEvent ce)
                {
                    jsplitpane.removeComponentListener(this);
                    setJSplitPaneDividerLocation(jsplitpane, proportionalLocation);
                }
            });
        }
    }
    else
    {
        jsplitpane.addHierarchyListener(new HierarchyListener()
        {
            @Override
            public void hierarchyChanged(HierarchyEvent e)
            {
                if ((e.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED) != 0 && jsplitpane.isShowing())
                {
                    jsplitpane.removeHierarchyListener(this);
                    setJSplitPaneDividerLocation(jsplitpane, proportionalLocation);
                }
            }
        });
    }
}
于 2012-02-14T19:00:20.323 回答
0

中的以下内容ComponentJSplitPane可以:

@Override
public void setVisible(final boolean b) {
    super.setVisible(b);
    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            AppFrame.this.jSplitPane.setDividerLocation(0.9d);
        }

    });
}
于 2012-08-01T08:00:35.153 回答
0

这里有一个解决方案,它是一个简单的函数,不需要对拆分窗格进行子分类或任何其他更改。

于 2011-06-13T00:07:18.747 回答