看了JSplitPane源代码后,自己已经解决!
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;public class SplitTest extends JFrame
{
public SplitTest()
{
super("SplitFrame Test");
init();
}
public void init()
{
Container c = getContentPane();
JTree tree = new JTree();
JPanel panel1 = new JPanel();
panel1.setBackground(Color.white);
JSplitPane centerPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,tree,panel1);
centerPane.setContinuousLayout(true);
centerPane.setOneTouchExpandable(true);
JPanel panel2 = new JPanel();
panel2.setBackground(Color.red);
JSplitPane mainPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,panel2,centerPane);
mainPane.setContinuousLayout(true);
c.add(mainPane,"Center");
this.setDefaultCloseOperation(3);
this.setSize(800,600);
this.show();
//======================================此处无效
centerPane.setDividerLocation(0.2);
//======================================
//======================================此处无效
mainPane.setDividerLocation(0.2);
//======================================
this.repaint();
}
public static void main(String args[])
{
new SplitTest();
}
}
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;public class SplitTest extends JFrame
{
public SplitTest()
{
super("SplitFrame Test");
init();
}
public void init()
{
Container c = getContentPane();
JTree tree = new JTree();
JPanel panel1 = new JPanel();
panel1.setBackground(Color.white);
JSplitPane centerPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,tree,panel1);
centerPane.setContinuousLayout(true);
centerPane.setOneTouchExpandable(true);
JPanel panel2 = new JPanel();
panel2.setBackground(Color.red);
JSplitPane mainPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,panel2,centerPane);
mainPane.setContinuousLayout(true);
c.add(mainPane,"Center");
this.setDefaultCloseOperation(3);
this.setSize(800,600);
this.show();
//======================================此处无效
centerPane.setDividerLocation(0.2);
//======================================
//======================================此处无效
mainPane.setDividerLocation(0.2);
//======================================
this.repaint();
}
public static void main(String args[])
{
new SplitTest();
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货