
レイアウトマネジャー
1 package com.tkadvance;
2
3 import java.awt.BorderLayout;
4
5 import javax.swing.JButton;
6 import javax.swing.JFrame;
7
8 public class SampleBorderLayout extends JFrame{
9
10 /**
11 *
12 */
13 private static final long serialVersionUID = 1L;
14
15 public SampleBorderLayout() {
16 // TODO 自動生成されたコンストラクター・スタブ
17 super();
18 setDefaultCloseOperation(EXIT_ON_CLOSE);
19 this.setTitle("サンプルBorderLayout");
20 JButton north = new JButton("NORTH");
21 JButton west = new JButton("WEST");
22 JButton south = new JButton("SOUTH");
23 JButton east = new JButton("EAST");
24 JButton center = new JButton("CENTER");
25 this.add(north, BorderLayout.NORTH);
26 this.add(west, BorderLayout.WEST);
27 this.add(south, BorderLayout.SOUTH);
28 this.add(east, BorderLayout.EAST);
29 this.add(center, BorderLayout.CENTER);
30 this.setSize(300,200);
31 this.setVisible(true);
32 }
33
34 /**
35 * @param args
36 */
37 public static void main(String[] args) {
38 // TODO 自動生成されたメソッド・スタブ
39 new SampleBorderLayout();
40 }
41
42 }
18. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//でJFrameをimportしているのでJFrameの箇所が省略されているのですね
実行後、ウィンドウの大きさを変更してみてください。
ウィンドウの大きさによってボタンのレイアウトが自動的に変更されますよね!
これはLayoutManagerが自動的にレイアウトを調整しているのですね
|