JSplitPane控件是Swing中的分块显示控件,它最多能显示,也只能显示2个控件。用户可以调整和操控这两个控件。
JSplitPane中的控件可以通过JSplitPane.HORIZONTAL_SPLIT属性设置从左到右;或JSplitPane.VERTICAL_SPLIT属性设置从上到下布置。
JSplitPane() JSplitPane(int newOrientation) JSplitPane(int newOrientation, boolean newContinuousLayout) JSplitPane(int newOrientation, boolean newContinuousLayout, Component newLeftComponent, Component newRightComponent) JSplitPane(int newOrientation, Component newLeftComponent, Component newRightComponent)
以上5个构造函数的中参数:
int newOrientation:设置水平显示还是垂直显示 JSplitPane.VERTICAL_SPLIT JSplitPane.HORIZONTAL_SPLIT boolean newContinuousLayout:设置在调整隔行大小时重绘还是调整隔行大小完毕时重绘 true 调整时重绘 false 调整完毕后重绘 newLeftComponent newRightComponent:要显示的控件
以上参数可以在构造时设置,也可在创建实例后再设置。
setContinuousLayout(boolean newContinuousLayout) setOrientation(int orientation) add(Component comp, int index)
具体事例
public class JSplitPane1 { public JSplitPane1() { JFrame f = new JFrame("JSplitPaneDemo"); Container contentPane = f.getContentPane(); JLabel label1 = new JLabel("Label 1",JLabel.CENTER); label1.setBackground(Color.green); label1.setOpaque(true); JLabel label2 = new JLabel("Label 2",JLabel.CENTER); label2.setBackground(Color.pink); label2.setOpaque(true); JLabel label3 = new JLabel("Label 3",JLabel.CENTER); label3.setBackground(Color.yellow); label3.setOpaque(true); //添加上滑动控件,并将2个label放置控件中 JSplitPane splitPane1 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, false, label1, label2); splitPane1.setDividerLocation(0.3); splitPane1.setOneTouchExpandable(true); splitPane1.setDividerSize(10); //添加主滑动控件,上面是滑动控件,下面是label /*JSplitPane splitPane2 = new JSplitPane(JSplitPane.VERTICAL_SPLIT, true, splitPane1, label3);*/ JSplitPane splitPane2 = new JSplitPane(); splitPane2.setContinuousLayout(true); splitPane2.setOrientation(JSplitPane.VERTICAL_SPLIT); splitPane2.add(splitPane1, JSplitPane.BOTTOM); splitPane2.add(label3, JSplitPane.TOP); splitPane2.setDividerLocation(35); splitPane2.setOneTouchExpandable(false); splitPane2.setDividerSize(5); contentPane.add(splitPane2); f.setSize(250,200); f.setVisible(true); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); } public static void main(String[] arg) { new JSplitPane1(); } }
时间: 2024-10-20 19:16:56