|
■ swingコンポーネント->JSplitPane |
| JSplitPaneはディバイダ(仕切線)によって領域を上下あるいは左右に分けて、それぞれにコンポーネントを表示させます。ディバイダをドラックして各コンポーネントの領域の大きさを変更することができます。 | |||||||||||||
|
|
|||||||||||||
|
|
|||||||||||||
| コンストラクタ(抜粋) | |||||||||||||
|
|||||||||||||
| メソッド(抜粋) | |||||||||||||
|
|||||||||||||
|
|
|||||||||||||
| SampleSplit.java | |||||||||||||
| import java.awt.*; | |||||||||||||
| import java.awt.event.*; | |||||||||||||
| import javax.swing.*; | |||||||||||||
| public class SampleSplit extends JFrame { | |||||||||||||
| Container contentPane; | |||||||||||||
| JSplitPane split; | |||||||||||||
| LeftButton leftbtn = new LeftButton(); | |||||||||||||
| RightScroll rhtscl = new RightScroll(); | |||||||||||||
| public SampleSplit() { | |||||||||||||
| super("SampleSplit"); | |||||||||||||
| addWindowAdapter(new WindowListener() { | |||||||||||||
| public void windowClosing(WindowEvent e) { | |||||||||||||
| System.exit(0); | |||||||||||||
| } | |||||||||||||
| }); | |||||||||||||
| contentPane = getContentPane(); | |||||||||||||
| contentPane.setLayout(new BorderLayout()); | |||||||||||||
| split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,leftbtn,rhtscl); | ← @インスタンス 生成 |
||||||||||||
| contentPane.add("Center",split); | |||||||||||||
| setSize(350,220); | |||||||||||||
| setVisible(true); | |||||||||||||
| } | |||||||||||||
| } | |||||||||||||
| public static void main(String[] args) { | |||||||||||||
| SampleSplit myClass = new SampleSplit(); | |||||||||||||
| } | |||||||||||||
| class LeftButton extends JPanel { | ← ALeftButton クラス |
||||||||||||
| private int NUM = 3; | |||||||||||||
| JButton[] btn = new JButton[NUM]; | |||||||||||||
| public LeftButton(){ | |||||||||||||
| setLayout(new GridLayout(3,1)); | |||||||||||||
| for(int i=0; i<NUM; i++){ | |||||||||||||
| btn[i] = new JButton("ボタン"+i); | |||||||||||||
| add(btn[i]); | |||||||||||||
| } | |||||||||||||
| } | |||||||||||||
| } | |||||||||||||
| class RightScroll extends JPanel { | ← BRightScroll クラス |
||||||||||||
| JTextArea textArea = new JTextArea(10,20); | |||||||||||||
| JScrollPane scl = new JScrollPane(textArea, | |||||||||||||
| ScrollPaneContents.VERTICAL_SCROLLBAR_ALWAYS, | |||||||||||||
| ScrollPaneContents.HORIZONTAL_SCROLLBAR_ALWAYS); | |||||||||||||
| public RightScroll(){ | |||||||||||||
| add(scl); | |||||||||||||
| } | |||||||||||||
| } | |||||||||||||
| } | |||||||||||||
| @split
= new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true,leftbtn,rhtscl); JSplitPaneのインスタンスを生成しています。 区切線を左右分割で、左側にLeftButtonを右側にRightScrollを設定しています。 continuousLayoutをtrueに設定しているので、ディバイダ(仕切線)をドラッグするたびに コンポーネントが再描画されます。 |
|||||||||||||
| Aclass
LeftButton extends JPanel {} JPanelを継承したLeftButtonクラスです。ボタン3つが配置されています。 |
|||||||||||||
| Bclass RightScroll extends
JPanel {} JPanelを継承したRightScrollクラスです。スクロールペインがついたテキストエリアが配置されています。 |
|||||||||||||
| もどる | |||||||||||||