JSplitPane SetDividerLocation问题

时间:2009-12-10 06:45:53

标签: java jsplitpane

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

现在,在给setDividerLocation提供0.5(如建议的)参数时,Java似乎将其视为正常数字而不是百分比。在中,分隔符,而不是到窗格的中间,几乎在左窗格的开头(窗格是垂直分割)。 有什么工作吗?

10 个答案:

答案 0 :(得分:28)

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

中有所描述

答案 1 :(得分:15)

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

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

答案 2 :(得分:7)

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

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();
  }
}

答案 3 :(得分: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;
        }
    }

}

答案 4 :(得分:1)

如果您对每次调整窗格大小时分隔符移动到中间感到高兴,可以添加ComponentListener并使其componentResized方法调用setDividerLocation(0.5)。

答案 5 :(得分:1)

这适用于我,基于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);
                }
            }
        });
    }
}

答案 6 :(得分:1)

使用setResizeWeight(double);

答案 7 :(得分:0)

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

答案 8 :(得分:0)

Component JSplitPane中的以下内容也是如此:

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

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

    });
}

答案 9 :(得分:0)

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

_splitPane.setResizeWeight(0.5);

SwingUtilities.invokeLater(new Runnable() {
    @Override
    public void run() {
        _splitPane.setDividerLocation(0.5);
    }
});