Swing レイアウト 2
ナビゲーションに移動
検索に移動
目次
Swing レイアウト 2
Swing |
- FlowLayout
- GridBagLayout
- GridLayout
FlowLayout
L to R
R to L
GridBagLayout
グリッドの位置
結果
GridLayout
ソースコード
import java.awt.BorderLayout; import java.awt.ComponentOrientation; import java.awt.Container; import java.awt.FlowLayout; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JSlider; import javax.swing.JTextField; import javax.swing.SwingUtilities; import javax.swing.UIManager; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; @SuppressWarnings("serial") public class LayoutTest2 extends JFrame { /** * FlowLayout test * @see http://java.sun.com/docs/books/tutorial/uiswing/layout/flow.html */ public void testFlowLayout() { JFrame frame = new JFrame("FlowLayout Test"); Container pane = frame.getContentPane(); final JPanel panel1 = new JPanel(new FlowLayout()); class MyMethodLocalActionListener implements ActionListener { MyMethodLocalActionListener() { } public void actionPerformed(ActionEvent e) { ComponentOrientation co = null; if ("L to R".equals(((JButton)e.getSource()).getText())) { co = ComponentOrientation.LEFT_TO_RIGHT; } else { co = ComponentOrientation.RIGHT_TO_LEFT; } panel1.setComponentOrientation(co); panel1.validate(); panel1.repaint(); } } JButton btn1 = new JButton("L to R"); btn1.addActionListener(new MyMethodLocalActionListener()); JButton btn2 = new JButton("R to L"); btn2.addActionListener(new MyMethodLocalActionListener()); panel1.add(btn1); panel1.add(new JTextField("text 1", 10)); panel1.add(new JTextField("text 2", 20)); panel1.add(btn2); pane.add(panel1); frame.pack(); frame.setVisible(true); } /** * GridBagLayout test * @see http://java.sun.com/docs/books/tutorial/uiswing/layout/gridbag.html */ public void testGridBagLayout() { JFrame frame = new JFrame("GridBagLayout Test"); Container pane = frame.getContentPane(); GridBagLayout gbl = new GridBagLayout(); pane.setLayout(gbl); GridBagConstraints c = new GridBagConstraints(); int[][] buttonPos = { {0,5}/*0*/, {0,4}/*1*/,{2,4}/*2*/,{4,4}/*3*/, {0,3}/*4*/,{2,3}/*5*/,{4,3}/*6*/, {0,2}/*7*/,{2,2}/*8*/,{4,2}/*9*/, }; c.fill = GridBagConstraints.HORIZONTAL; // 共通設定 c.gridwidth = 6; // ↓ 列数設定 c.gridx = 0; c.gridy = 0; c.gridwidth = GridBagConstraints.REMAINDER; // ↑ 最終行 JTextField txtResult = new JTextField(""); gbl.setConstraints(txtResult, c); pane.add(txtResult); c.gridwidth = 3; // ↓ 列数設定 c.gridx = 0; c.gridy = 1; JButton btnCE = new JButton("CE"); gbl.setConstraints(btnCE, c); pane.add(btnCE); c.gridx = 3; c.gridy = 1; c.gridwidth = GridBagConstraints.REMAINDER; // ↑ 最終行 JButton btnC = new JButton("C"); gbl.setConstraints(btnC, c); pane.add(btnC); c.gridwidth = 2; // ↓ 列数設定 for (int i=0; i<10; i++) { c.gridx = buttonPos[i][0]; c.gridy = buttonPos[i][1]; JButton btnN = new JButton(String.valueOf(i)); gbl.setConstraints(btnN, c); pane.add(btnN); } c.gridx = 2; c.gridy = 5; JButton btnP = new JButton("+"); gbl.setConstraints(btnP, c); pane.add(btnP); c.gridx = 4; c.gridy = 5; JButton btnM = new JButton("-"); gbl.setConstraints(btnM, c); pane.add(btnM); frame.pack(); frame.setVisible(true); } /** * GridLayout test * @see http://java.sun.com/docs/books/tutorial/uiswing/layout/grid.html */ public void testGridLayout() { JFrame frame = new JFrame("GridLayout Test"); final Container pane = frame.getContentPane(); pane.setLayout(new BorderLayout()); final GridLayout gridLayout = new GridLayout(3,3); final JPanel gridPanel = new JPanel(gridLayout); for (int i=0; i<9; i++) { gridPanel.add(new JButton(String.valueOf(i))); } JPanel ctrlPanel = new JPanel(new BorderLayout()); JSlider slider = new JSlider(0 ,50); slider.addChangeListener( new ChangeListener() { public void stateChanged(ChangeEvent e) { int val = ((JSlider)e.getSource()).getValue(); gridLayout.setHgap(val); gridLayout.setVgap(val); gridLayout.layoutContainer(gridPanel); } } ); ctrlPanel.add(new JLabel("gap 0 - 50"), BorderLayout.NORTH); ctrlPanel.add(slider, BorderLayout.CENTER); pane.add(ctrlPanel, BorderLayout.NORTH); pane.add(gridPanel, BorderLayout.CENTER); frame.pack(); frame.setVisible(true); } /* ******* 以下メニュー用 ********* */ public LayoutTest2() { setTitle("Layout Test 2"); JButton btn1 = new JButton("flow"); btn1.addActionListener(new MyActionListener("flow")); JButton btn2 = new JButton("gridbag"); btn2.addActionListener(new MyActionListener("gridbag")); JButton btn3 = new JButton("grid"); btn3.addActionListener(new MyActionListener("grid")); JPanel pane = new JPanel(); pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS)); pane.add(btn1); pane.add(btn2); pane.add(btn3); getContentPane().add(pane); pack(); } class MyActionListener implements ActionListener { private String type = null; public MyActionListener(String type) { this.type = type; } public void actionPerformed(ActionEvent e) { if ("flow".equals(this.type)) { testFlowLayout(); } if ("gridbag".equals(this.type)) { testGridBagLayout(); } if ("grid".equals(this.type)) { testGridLayout(); } } } public static void main(String[] args) { try { UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); // UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel"); } catch (Exception e) { e.printStackTrace(); } SwingUtilities.invokeLater( new Runnable() { public void run() { LayoutTest2 lt = new LayoutTest2(); lt.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); lt.setVisible(true); } } ); } }
© 2006 矢木浩人