スプリットペインの中にスプリットペインを配置する

広告

スプリットペイン自身もコンポーネントですのでスプリットペインの左右の領域に他のスプリットペインを配置することも可能です。スプリットペインをネストして利用することでより複雑なレイアウトが可能となります。

次のサンプルでは画面を3分割して利用するようにスプリットペインをネストして配置してあります。

JSplitPaneTest13.java

import javax.swing.*;
import java.awt.BorderLayout;

public class JSplitPaneTest13 extends JFrame{

  public static void main(String[] args){
    JSplitPaneTest13 frame = new JSplitPaneTest13();

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setBounds(10, 10, 300, 200);
    frame.setTitle("タイトル");
    frame.setVisible(true);
  }

  JSplitPaneTest13(){
    JSplitPane splitpane = new JSplitPane();

    JPanel leftPanel = new JPanel();
    JButton leftButton = new JButton("Left");
    leftPanel.add(leftButton);

    JSplitPane rightSplitpane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);

    JPanel topPanel = new JPanel();
    JButton topButton = new JButton("Top");
    topPanel.add(topButton);

    JPanel bottomPanel = new JPanel();
    JButton bottomButton = new JButton("Bottom");
    bottomPanel.add(bottomButton);

    rightSplitpane.setLeftComponent(topPanel);
    rightSplitpane.setRightComponent(bottomPanel);

    splitpane.setLeftComponent(leftPanel);
    splitpane.setRightComponent(rightSplitpane);

    getContentPane().add(splitpane, BorderLayout.CENTER);
  }
}

上記をコンパイルした後で実行すると次のように表示されます。

JSplitPaneでネストして利用する

( Written by Tatsuo Ikura )

Profile
profile_img

著者 / TATSUO IKURA

プログラミングや開発環境構築の解説サイトを運営しています。