JSplitPane SetDividerLocation问题

Gou*_*ham 15 java jsplitpane

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

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

Sam*_*ley 28

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


cam*_*ckr 17

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

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


Sin*_*hot 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();
  }
}
Run Code Online (Sandbox Code Playgroud)