Java學習:JSplitPane控件

JSplitPane控件是Swing中的分塊顯示控件,它最多能顯示,也只能顯示2個控件。用戶能夠調整和操控這兩個控件。java

JSplitPane中的控件能夠通JSplitPane.HORIZONTAL_SPLIT屬性設置從左到右;或JSplitPane.VERTICAL_SPLIT屬性設置從上到下佈置。ide

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:要顯示的控件

以上參數能夠在構造時設置,也可在建立實例後再設置。
spa

setContinuousLayout(boolean newContinuousLayout)

setOrientation(int orientation)

add(Component comp, int index)

具體事例
code

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();
    }
}
相關文章
相關標籤/搜索