【Java】【Swing】 レイアウト [2] ~ GroupLayout編 ~

サンプル

例1:GroupLayoutDemo.java

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.SequentialGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class GroupLayoutDemo {

  public static void main(String[] args) {
    JFrame frame = new JFrame("GroupLayout Demo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel panel = new JPanel();
    JLabel lastNameLabel = new JLabel("Last Name :");
    JLabel firstNameLabel = new JLabel("First Name : ");
    JTextField lastNameField = new JTextField(10);
    JTextField firstNameField = new JTextField(10);

    // GroupLayout
    GroupLayout groupLayout = new GroupLayout(panel);
    panel.setLayout(groupLayout);

    // 水平方向のグループ
    SequentialGroup horizontalGroup = groupLayout.createSequentialGroup();
    horizontalGroup.addGroup(groupLayout.createParallelGroup().addComponent(lastNameLabel).addComponent(firstNameLabel));
    horizontalGroup.addGroup(groupLayout.createParallelGroup().addComponent(lastNameField).addComponent(firstNameField));
    groupLayout.setHorizontalGroup(horizontalGroup);

    // 垂直方向のグループ
    SequentialGroup verticalGroup = groupLayout.createSequentialGroup();
    verticalGroup.addGroup(groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(lastNameLabel)
        .addComponent(lastNameField));
    verticalGroup.addGroup(groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(firstNameLabel)
        .addComponent(firstNameField));
    groupLayout.setVerticalGroup(verticalGroup);

    frame.add(panel);
    frame.pack();
    frame.setVisible(true);
  }
}

例2:GroupLayoutDemo2.java

グループ項目を線引きして表示
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;

public class GroupLayoutDemo2 {

  public static void main(String[] args) {
    JFrame frame = new JFrame("GroupLayout Demo2");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // GridBagLayout
    JPanel panel = new JPanel(new GridBagLayout());
    JTextField textField1 = new JTextField();
    JTextField textField2 = new JTextField();

    Border inside = BorderFactory.createEmptyBorder(10, 5 + 2, 10, 10 + 2);
    Border outside = BorderFactory.createTitledBorder("Group Title");
    panel.setBorder(BorderFactory.createCompoundBorder(outside, inside));
    GridBagConstraints gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.gridheight = 1;

    gridBagConstraints.gridx = 0;
    gridBagConstraints.insets = new Insets(5, 5, 5, 0);
    gridBagConstraints.anchor = GridBagConstraints.WEST;
    gridBagConstraints.gridy = 0;
    panel.add(new JLabel("Item1 :"), gridBagConstraints);
    gridBagConstraints.gridy = 1;
    panel.add(new JLabel("Item2 :"), gridBagConstraints);

    gridBagConstraints.gridx = 1;
    gridBagConstraints.weightx = 1.0;
    gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
    gridBagConstraints.gridy = 0;
    panel.add(textField1, gridBagConstraints);
    gridBagConstraints.gridy = 1;
    panel.add(textField2, gridBagConstraints);

    frame.add(panel);
    frame.pack();
    frame.setVisible(true);
  }
}


関連記事

Java】【Swing】 レイアウト [1] ~ 基本編 ~

http://blogs.yahoo.co.jp/dk521123/36548731.html

Java】【Swing】 レイアウト [2] ~ GroupLayout編 ~

http://blogs.yahoo.co.jp/dk521123/36548707.html

Java】【Swing】 レイアウト [3] ~ GridBagLayout 編 ~

http://blogs.yahoo.co.jp/dk521123/36548823.html

Java】【Swing】 レイアウト [4] ~ BoxLayout 編 ~

http://blogs.yahoo.co.jp/dk521123/36553511.html