「Swing レイアウト 2」の版間の差分
ナビゲーションに移動
検索に移動
(同じ利用者による、間の1版が非表示) | |||
1行目: | 1行目: | ||
− | ==Swing レイアウト 2== | + | ==[[Swing レイアウト 2]]== |
− | [[Swing]] | + | [[Swing]] | |
*FlowLayout | *FlowLayout | ||
7行目: | 7行目: | ||
====FlowLayout==== | ====FlowLayout==== | ||
− | =====L to R===== | + | =====L to [[R]]===== |
[[File:1198_flow01.jpg]] | [[File:1198_flow01.jpg]] | ||
− | =====R to L===== | + | =====[[R]] to L===== |
[[File:1199_flow02.jpg]] | [[File:1199_flow02.jpg]] | ||
====GridBagLayout==== | ====GridBagLayout==== | ||
34行目: | 34行目: | ||
import javax.swing.BoxLayout; | import javax.swing.BoxLayout; | ||
import javax.swing.JButton; | import javax.swing.JButton; | ||
− | import javax.swing.JFrame; | + | import javax.swing.[[JFrame]]; |
import javax.swing.JLabel; | import javax.swing.JLabel; | ||
import javax.swing.JPanel; | import javax.swing.JPanel; | ||
import javax.swing.JSlider; | import javax.swing.JSlider; | ||
import javax.swing.JTextField; | import javax.swing.JTextField; | ||
− | import javax.swing. | + | import javax.swing.[[Swing]]Utilities; |
import javax.swing.UIManager; | import javax.swing.UIManager; | ||
import javax.swing.event.ChangeEvent; | import javax.swing.event.ChangeEvent; | ||
45行目: | 45行目: | ||
@SuppressWarnings("serial") | @SuppressWarnings("serial") | ||
− | public class LayoutTest2 extends JFrame { | + | public class LayoutTest2 extends [[JFrame]] { |
/** | /** | ||
* FlowLayout test | * FlowLayout test | ||
51行目: | 51行目: | ||
*/ | */ | ||
public void testFlowLayout() { | public void testFlowLayout() { | ||
− | JFrame frame = new JFrame("FlowLayout Test"); | + | [[JFrame]] frame = new [[JFrame]]("FlowLayout Test"); |
Container pane = frame.getContentPane(); | Container pane = frame.getContentPane(); | ||
61行目: | 61行目: | ||
public void actionPerformed(ActionEvent e) { | public void actionPerformed(ActionEvent e) { | ||
ComponentOrientation co = null; | ComponentOrientation co = null; | ||
− | if ("L to R".equals(((JButton)e.getSource()).getText())) { | + | if ("L to [[R]]".equals(((JButton)e.getSource()).getText())) { |
− | co = ComponentOrientation. | + | co = ComponentOrientation.LEFT_TO_[[R]]IGHT; |
} else { | } else { | ||
− | co = ComponentOrientation. | + | co = ComponentOrientation.[[R]]IGHT_TO_LEFT; |
} | } | ||
panel1.setComponentOrientation(co); | panel1.setComponentOrientation(co); | ||
72行目: | 72行目: | ||
} | } | ||
− | JButton btn1 = new JButton("L to R"); | + | JButton btn1 = new JButton("L to [[R]]"); |
btn1.addActionListener(new MyMethodLocalActionListener()); | btn1.addActionListener(new MyMethodLocalActionListener()); | ||
− | JButton btn2 = new JButton("R to L"); | + | JButton btn2 = new JButton("[[R]] to L"); |
btn2.addActionListener(new MyMethodLocalActionListener()); | btn2.addActionListener(new MyMethodLocalActionListener()); | ||
92行目: | 92行目: | ||
*/ | */ | ||
public void testGridBagLayout() { | public void testGridBagLayout() { | ||
− | JFrame frame = new JFrame("GridBagLayout Test"); | + | [[JFrame]] frame = new [[JFrame]]("GridBagLayout Test"); |
Container pane = frame.getContentPane(); | Container pane = frame.getContentPane(); | ||
105行目: | 105行目: | ||
{0,2}/*7*/,{2,2}/*8*/,{4,2}/*9*/, | {0,2}/*7*/,{2,2}/*8*/,{4,2}/*9*/, | ||
}; | }; | ||
− | c.fill = GridBagConstraints. | + | c.fill = GridBagConstraints.HO[[R]]IZONTAL; // 共通設定 |
c.gridwidth = 6; // ↓ 列数設定 | c.gridwidth = 6; // ↓ 列数設定 | ||
c.gridx = 0; | c.gridx = 0; | ||
c.gridy = 0; | c.gridy = 0; | ||
− | c.gridwidth = GridBagConstraints. | + | c.gridwidth = GridBagConstraints.[[R]]EMAINDE[[R]]; // ↑ 最終行 |
− | JTextField | + | JTextField txt[[R]]esult = new JTextField(""); |
− | gbl.setConstraints( | + | gbl.setConstraints(txt[[R]]esult, c); |
− | pane.add( | + | pane.add(txt[[R]]esult); |
c.gridwidth = 3; // ↓ 列数設定 | c.gridwidth = 3; // ↓ 列数設定 | ||
124行目: | 124行目: | ||
c.gridx = 3; | c.gridx = 3; | ||
c.gridy = 1; | c.gridy = 1; | ||
− | c.gridwidth = GridBagConstraints. | + | c.gridwidth = GridBagConstraints.[[R]]EMAINDE[[R]]; // ↑ 最終行 |
JButton btnC = new JButton("C"); | JButton btnC = new JButton("C"); | ||
gbl.setConstraints(btnC, c); | gbl.setConstraints(btnC, c); | ||
158行目: | 158行目: | ||
*/ | */ | ||
public void testGridLayout() { | public void testGridLayout() { | ||
− | JFrame frame = new JFrame("GridLayout Test"); | + | [[JFrame]] frame = new [[JFrame]]("GridLayout Test"); |
final Container pane = frame.getContentPane(); | final Container pane = frame.getContentPane(); | ||
pane.setLayout(new BorderLayout()); | pane.setLayout(new BorderLayout()); | ||
180行目: | 180行目: | ||
} | } | ||
); | ); | ||
− | ctrlPanel.add(new JLabel("gap 0 - 50"), BorderLayout. | + | ctrlPanel.add(new JLabel("gap 0 - 50"), BorderLayout.NO[[R]]TH); |
− | ctrlPanel.add(slider, BorderLayout. | + | ctrlPanel.add(slider, BorderLayout.CENTE[[R]]); |
− | pane.add(ctrlPanel, BorderLayout. | + | pane.add(ctrlPanel, BorderLayout.NO[[R]]TH); |
− | pane.add(gridPanel, BorderLayout. | + | pane.add(gridPanel, BorderLayout.CENTE[[R]]); |
frame.pack(); | frame.pack(); | ||
frame.setVisible(true); | frame.setVisible(true); | ||
231行目: | 231行目: | ||
public static void main(String[] args) { | public static void main(String[] args) { | ||
try { | try { | ||
− | UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows. | + | UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.[[Windows]]LookAndFeel"); |
// UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel"); | // UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel"); | ||
} catch (Exception e) { | } catch (Exception e) { | ||
e.printStackTrace(); | e.printStackTrace(); | ||
} | } | ||
− | + | [[Swing]]Utilities.invokeLater( | |
− | new | + | new [[R]]unnable() { |
public void run() { | public void run() { | ||
LayoutTest2 lt = new LayoutTest2(); | LayoutTest2 lt = new LayoutTest2(); | ||
− | lt.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | + | lt.setDefaultCloseOperation([[JFrame]].EXIT_ON_CLOSE); |
lt.setVisible(true); | lt.setVisible(true); | ||
} | } |
2020年2月16日 (日) 04:32時点における最新版
目次
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 矢木浩人