//
首先,在JFrame 显示出来的时候,为什么bottom.setDividerLocation(0.5);
显示不出来正确的位置,当点击按钮
JButton b=new JButton("real divider");
就能正确显示分割线的位置
代码如下:
//
package swingTest;
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2006</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class ScrollPaneTest {
public static void main(String[] arg){
JFrame f=new JFrame();
f.setSize(600,500);
f.setLocation(250,250);
Container c= f.getContentPane();
c.setLayout(new BorderLayout());
final JSplitPane left=new JSplitPane(JSplitPane.VERTICAL_SPLIT,new JPanel(),new JPanel());JPanel p1=new JPanel();
JButton b=new JButton("real divider");
p1.add(b);JPanel p2=new JPanel();
JPanel p3=new JPanel();
final JSplitPane bottom=new JSplitPane(JSplitPane.VERTICAL_SPLIT,p2,p3);
final JSplitPane right=new JSplitPane(JSplitPane.VERTICAL_SPLIT,p1,bottom);
JPanel leftPanel=new JPanel(new BorderLayout());
// leftPanel.add(left,BorderLayout.CENTER);
final JSplitPane top=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,left,right);
c.add(top,BorderLayout.CENTER);f.setVisible(true);top.setDividerLocation(0.7);
right.setDividerLocation(0.3);
bottom.setDividerLocation(0.5);
left.setDividerLocation(0.8);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
top.setDividerLocation(0.7);
right.setDividerLocation(0.3);
bottom.setDividerLocation(0.5);
left.setDividerLocation(0.8);
}
});
}
}

解决方案 »

  1.   

    把bottom.setDividerLocation(0.5);移到top.setDividerLocation(0.7);前面。
    至于原因,自己想。
      

  2.   

    JSplitPane有个问题,就是你在setVisible()之前设置的DividerLocation是用width=0计算出整数值的DividerLocation,因此很有可能不正确。
    解决是用重写paint方法。但不是100%正确,你看看setDividerLocation(double)的代码就知道这是什么鸟问题了。