『とほほのJava入門』「レイアウト グリッドバッグレイアウト」
『とほほのJava入門』を読んでいるところ
19. レイアウト の「グリッドバッグレイアウト」
AWT で、
import java.awt.*; public class C2008021200 extends Frame { GridBagLayout gbl = new GridBagLayout(); void addLabel(Label label, int x, int y, int w, int h) { GridBagConstraints gbc = new GridBagConstraints(); gbc.fill = GridBagConstraints.BOTH; gbc.gridx = x; gbc.gridy = y; gbc.gridwidth = w; gbc.gridheight = h; gbl.setConstraints(label, gbc); add(label); } C2008021200() { super("GridBagLayoutTest"); setSize(200, 100); Label l1 = new Label("Label1"); Label l2 = new Label("Label2"); Label l3 = new Label("Label3"); Label l4 = new Label("Label4"); setLayout(gbl); addLabel(l1, 0, 0, 1, 3); addLabel(l2, 1, 0, 1, 1); addLabel(l3, 1, 1, 1, 1); addLabel(l4, 1, 2, 1, 1); show(); } public static void main(String [] args) { new C2008021200(); } }
Swing で、
import java.awt.*; import javax.swing.*; class C2008021201 extends JFrame { GridBagLayout gbl = new GridBagLayout(); void addLabel(JLabel label, int x, int y, int w, int h) { GridBagConstraints gbc = new GridBagConstraints(); gbc.fill = GridBagConstraints.BOTH; gbc.gridx = x; gbc.gridy = y; gbc.gridwidth = w; gbc.gridheight = h; gbl.setConstraints(label, gbc); getContentPane().add(label); } C2008021201() { JLabel label1 = new JLabel("Label1"); JLabel label2 = new JLabel("Label2"); JLabel label3 = new JLabel("Label3"); JLabel label4 = new JLabel("Label4"); getContentPane().setLayout(gbl); addLabel(label1, 0, 0, 1, 3); addLabel(label2, 1, 0, 1, 1); addLabel(label3, 1, 1, 1, 1); addLabel(label4, 1, 2, 1, 1); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setTitle("GridBagLayoutTest"); setSize(200, 100); setVisible(true); } public static void main(String[] args) { new C2008021201(); } }