トップ 新規 編集 差分 一覧 ソース 検索 ヘルプ PDF RSS ログイン

Swingの基本:GUI部品のレイアウト

@書き途中@

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package sample.swing.layout;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.LayoutManager;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

@SuppressWarnings("serial")
public class LayoutSample extends JFrame {
    private class TitledPanel extends JPanel {
        public TitledPanel(String title, LayoutManager layout) {
            if(layout==null) {
                layout=new GroupLayout(this);
            }
            setLayout(layout);
            setBorder(new TitledBorder(BorderFactory.createEtchedBorder(),title));
        }
    }
    public LayoutSample() {
        setLayout(new GridLayout(3,2));
        //BorderLayout・・・JFrameのデフォルトのLayoutManager
        TitledPanel borderLayout = new TitledPanel("BorderLayout",new BorderLayout());
        // BorderLayoutでは、方角(NORTH、SOUTH、WEST、EAST、CENTER)でGUI部品の配置を設定する
        borderLayout.add(new JButton("NORTH"),BorderLayout.NORTH);
        borderLayout.add(new JButton("SOUTH"),BorderLayout.SOUTH);
        borderLayout.add(new JButton("WEST"),BorderLayout.WEST);
        borderLayout.add(new JButton("EAST"),BorderLayout.EAST);
        borderLayout.add(new JButton("CENTER"),BorderLayout.CENTER);
        add(borderLayout);
        //GridLayout
        // GridLayoutは指定した数のグリッド(表のようなもの)を作成し、そこに配置していく
        TitledPanel gridLayout = new TitledPanel("GridLayout",new GridLayout(3,3));
        for(int i=1;i<=3;i++){
            for(int j=1;j<=3;j++){
                gridLayout.add(new JButton(i+":"+j));
            }
        }
        add(gridLayout);
        //FlowLayout
        TitledPanel flowLayout = new TitledPanel("FlowLayout",new FlowLayout());
        for(int i=1;i<10;i++){
            flowLayout.add(new JButton(""+i));
        }
        add(flowLayout);
        //BoxLayout
        TitledPanel boxLayout = new TitledPanel("BoxLayout",new FlowLayout());
        TitledPanel xAxis = new TitledPanel("X_AXIS",new FlowLayout());
        xAxis.setLayout(new BoxLayout(xAxis,BoxLayout.X_AXIS));
        for(int i=1;i<5;i++){
            xAxis.add(new JButton(""+i));
        }
        boxLayout.add(xAxis);
        TitledPanel yAxis = new TitledPanel("Y_AXIS",new FlowLayout());
        yAxis.setLayout(new BoxLayout(yAxis,BoxLayout.Y_AXIS));
        for(int i=1;i<5;i++){
            yAxis.add(new JButton(""+i));
        }
        boxLayout.add(yAxis);
        add(boxLayout);
        //GridBagLayout
        // 複雑な配置も可能なレイアウトだが設定も複雑
        TitledPanel gridBagLayout = new TitledPanel("GridBagLayout",new GridBagLayout());
        GridBagConstraints constraints = new GridBagConstraints();
        for(int i=1;i<10;i++) {
            JButton button = new JButton(""+i);
            constraints.gridx = i;
            constraints.gridy = i;
            constraints.gridwidth = -((i%2)-1);
            constraints.gridheight = i%2;
            gridBagLayout.add(button,constraints);
        }
        add(gridBagLayout);
        //GroupLayout
        // どちらかと言えば開発環境に任せるタイプのレイアウト
        TitledPanel groupLayout = new TitledPanel("GroupLayout",null);
        JLabel nameLabel = new JLabel("名前");
        JTextField nameField = new JTextField();
        JButton buttonX = new JButton("ボタン1");
        JLabel addressLabel = new JLabel("住所");
        JTextField addressField = new JTextField();
        JButton buttonY = new JButton("ボタン2");
        GroupLayout layout = (GroupLayout)groupLayout.getLayout();
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);

        layout.setHorizontalGroup(
            layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup()
                    .addComponent(nameLabel)
                    .addComponent(addressLabel))
                .addGroup(layout.createParallelGroup()
                    .addComponent(nameField)
                    .addComponent(addressField))
                .addGroup(layout.createParallelGroup()
                    .addComponent(buttonX)
                    .addComponent(buttonY)));
        layout.setVerticalGroup(
            layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(Alignment.BASELINE)
                    .addComponent(nameLabel)
                    .addComponent(nameField)
                    .addComponent(buttonX))
                .addGroup(layout.createParallelGroup(Alignment.BASELINE)
                    .addComponent(addressLabel)
                    .addComponent(addressField)
                    .addComponent(buttonY)));
        add(groupLayout);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // ×を押したときに終了するように
        setSize(800, 600);
        setVisible(true);
    }
    public static void main(String[] args) {
        new LayoutSample();
    }
}
LayoutSample.java

最終更新時間:2012年07月12日 22時24分37秒