java JSplitPane SetDividerLocation 问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1879091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSplitPane SetDividerLocation Problem
提问by Goutham
I have a JSplitPane which when shown should split the pane by 50%.
我有一个 JSplitPane,显示时应将窗格拆分 50%。
Now on giving an argument of 0.5 (as suggested) to setDividerLocation, Java seems to treat it as a normal number instead of a percentage. As in, the divider, instead of going to the middle of the pane, is almost at the start of the left pane (the pane is vertically split). Any work arounds?
现在,在为 setDividerLocation 提供 0.5(如建议的)参数时,Java 似乎将其视为正常数字而不是百分比。如在,分隔线,而不是去窗格的中间,几乎在左窗格的开始(窗格是垂直分割的)。任何解决方法?
回答by Sam Dealey
Am I missing something? There seem to be a lot of rather convoluted answers to this question... but I think a simple setResizeWeight(0.5) would solve the issue ... it's described in the SplitPane Tutorial
我错过了什么吗?这个问题似乎有很多相当复杂的答案......但我认为一个简单的 setResizeWeight(0.5) 可以解决这个问题......它在SplitPane 教程中有描述
回答by camickr
The setDividerLocation( double ) method only works on a "realized" frame, which means after you've packed or made the frame visible.
setDividerLocation( double ) 方法仅适用于“已实现”的框架,这意味着在您打包或使框架可见之后。
The setDividerLocation( int ) method can be used at any time.
setDividerLocation( int ) 方法可以随时使用。
回答by SingleShot
You can only set the split pane divider's location as a percentage when the split pane is visible. You can tap into the split pane owner's events to determine when its OK to set the divider's location. For example:
当拆分窗格可见时,您只能将拆分窗格分隔符的位置设置为百分比。您可以点击拆分窗格所有者的事件以确定何时可以设置分隔符的位置。例如:
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();
}
}
回答by david_p
this fixes it:
这修复了它:
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;
}
}
}
回答by lins314159
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).
如果您对每次调整窗格大小时分隔线移动到中间感到高兴,您可以添加一个 ComponentListener 并让它的 componentResized 方法调用 setDividerLocation(0.5)。
回答by HymanCY
This works for me, based on Dave Rays link.
这对我有用,基于 Dave Rays链接。
/**
* 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);
}
}
});
}
}
回答by alaster
use setResizeWeight(double);
利用 setResizeWeight(double);
回答by Cord Rehn
I found the combination of setResizeWeightand setDividerLocationinvoked later provides the behavior one would expect:
我发现setResizeWeight和setDividerLocation后来调用的组合提供了人们期望的行为:
_splitPane.setResizeWeight(0.5);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
_splitPane.setDividerLocation(0.5);
}
});
回答by rodhel
The following in the Componentwith the JSplitPanedoes it too:
Componentwith 中的以下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);
}
});
}

