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

If you're happy for the divider to move to the middle every time you resize the pane, you could add a ComponentListener and have its componentResized method call setDividerLocation(0.5).

于 2009-12-10T07:22:22.893 に答える
2

使用するsetResizeWeight(double);

于 2012-05-09T17:24:32.530 に答える
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

ここには、サブクラス化や分割ペインへのその他の変更を必要としない単純な関数である解決策があります。

于 2011-06-13T00:07:18.747 に答える
0

の次のComponentwithJSplitPaneもそれを行います。

@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 に答える