「Swing レイアウト 1」の版間の差分
ナビゲーションに移動
検索に移動
31行目: | 31行目: | ||
import javax.swing.JButton; | import javax.swing.JButton; | ||
import javax.swing.JComboBox; | import javax.swing.JComboBox; | ||
− | 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; | ||
42行目: | 42行目: | ||
@SuppressWarnings("serial") | @SuppressWarnings("serial") | ||
− | public class LayoutTest extends JFrame { | + | public class LayoutTest extends [[JFrame]] { |
/** | /** | ||
* BorderLayout test | * BorderLayout test | ||
48行目: | 48行目: | ||
*/ | */ | ||
public void testBorderLayout() { | public void testBorderLayout() { | ||
− | JFrame frame = new JFrame("BorderLayout Test"); | + | [[JFrame]] frame = new [[JFrame]]("BorderLayout Test"); |
Container pane = frame.getContentPane(); | Container pane = frame.getContentPane(); | ||
pane.setLayout(new BorderLayout()); | pane.setLayout(new BorderLayout()); | ||
− | pane.add(new JButton(" | + | pane.add(new JButton("NO[[R]]TH"), BorderLayout.NO[[R]]TH); |
pane.add(new JButton("WEST"), BorderLayout.WEST); | pane.add(new JButton("WEST"), BorderLayout.WEST); | ||
− | pane.add(new JButton(" | + | pane.add(new JButton("CENTE[[R]]"), BorderLayout.CENTE[[R]]); |
pane.add(new JButton("EAST"), BorderLayout.EAST); | pane.add(new JButton("EAST"), BorderLayout.EAST); | ||
pane.add(new JButton("SOUTH"), BorderLayout.SOUTH); | pane.add(new JButton("SOUTH"), BorderLayout.SOUTH); | ||
67行目: | 67行目: | ||
*/ | */ | ||
public void testBoxLayout() { | public void testBoxLayout() { | ||
− | JFrame frame = new JFrame("BoxLayout Test"); | + | [[JFrame]] frame = new [[JFrame]]("BoxLayout Test"); |
Container pane = frame.getContentPane(); | Container pane = frame.getContentPane(); | ||
73行目: | 73行目: | ||
JButton btn1 = new JButton("button1"); | JButton btn1 = new JButton("button1"); | ||
− | btn1.setAlignmentX(Component. | + | btn1.setAlignmentX(Component.CENTE[[R]]_ALIGNMENT); // 0.5f |
JButton btn2 = new JButton("button2"); | JButton btn2 = new JButton("button2"); | ||
− | btn2.setAlignmentX(Component. | + | btn2.setAlignmentX(Component.CENTE[[R]]_ALIGNMENT); // 0.5f |
JButton btn3 = new JButton("button3"); | JButton btn3 = new JButton("button3"); | ||
− | btn3.setAlignmentX(Component. | + | btn3.setAlignmentX(Component.CENTE[[R]]_ALIGNMENT); // 0.5f |
pane.add(btn1); | pane.add(btn1); | ||
125行目: | 125行目: | ||
*/ | */ | ||
public void testCardLayout() { | public void testCardLayout() { | ||
− | JFrame frame = new JFrame("CardLayout Test"); | + | [[JFrame]] frame = new [[JFrame]]("CardLayout Test"); |
Container pane = frame.getContentPane(); | Container pane = frame.getContentPane(); | ||
− | final String | + | final String BUTTON_CA[[R]]D = "buttons"; |
− | final String | + | final String TEXT_CA[[R]]D = "texts"; |
final JPanel cards = new JPanel(new CardLayout()); | final JPanel cards = new JPanel(new CardLayout()); | ||
160行目: | 160行目: | ||
card2.add(new JTextField("TextField", 10)); | card2.add(new JTextField("TextField", 10)); | ||
− | cards.add(card1, | + | cards.add(card1, BUTTON_CA[[R]]D); |
− | cards.add(card2, | + | cards.add(card2, TEXT_CA[[R]]D); |
− | pane.add(ctrlPane, BorderLayout. | + | pane.add(ctrlPane, BorderLayout.NO[[R]]TH); |
− | pane.add(cards, BorderLayout. | + | pane.add(cards, BorderLayout.CENTE[[R]]); |
frame.pack(); | frame.pack(); | ||
212行目: | 212行目: | ||
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() { | ||
LayoutTest lt = new LayoutTest(); | LayoutTest lt = new LayoutTest(); | ||
− | lt.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | + | lt.setDefaultCloseOperation([[JFrame]].EXIT_ON_CLOSE); |
lt.setVisible(true); | lt.setVisible(true); | ||
} | } |
2020年2月16日 (日) 04:32時点における最新版
レイアウト 1
Swing |
- BorderLayout
- BoxLayout
- CardLayout
BorderLayout
BoxLayout
CardLayout
1枚目
2枚目
ソースコード
import java.awt.BorderLayout; import java.awt.CardLayout; import java.awt.Component; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.util.Dictionary; import java.util.Hashtable; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JComboBox; 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 LayoutTest extends JFrame { /** * BorderLayout test * @see http://java.sun.com/docs/books/tutorial/uiswing/layout/border.html */ public void testBorderLayout() { JFrame frame = new JFrame("BorderLayout Test"); Container pane = frame.getContentPane(); pane.setLayout(new BorderLayout()); pane.add(new JButton("NORTH"), BorderLayout.NORTH); pane.add(new JButton("WEST"), BorderLayout.WEST); pane.add(new JButton("CENTER"), BorderLayout.CENTER); pane.add(new JButton("EAST"), BorderLayout.EAST); pane.add(new JButton("SOUTH"), BorderLayout.SOUTH); frame.pack(); frame.setVisible(true); } /** * BoxLayout test * @see http://java.sun.com/docs/books/tutorial/uiswing/layout/box.html */ public void testBoxLayout() { JFrame frame = new JFrame("BoxLayout Test"); Container pane = frame.getContentPane(); pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS)); JButton btn1 = new JButton("button1"); btn1.setAlignmentX(Component.CENTER_ALIGNMENT); // 0.5f JButton btn2 = new JButton("button2"); btn2.setAlignmentX(Component.CENTER_ALIGNMENT); // 0.5f JButton btn3 = new JButton("button3"); btn3.setAlignmentX(Component.CENTER_ALIGNMENT); // 0.5f pane.add(btn1); pane.add(btn2); pane.add(btn3); JPanel ctrlPanel = new JPanel(); Dictionary<Integer, JLabel> dic = new Hashtable<Integer, JLabel>(); dic.put(0, new JLabel("0.0f")); dic.put(5, new JLabel("0.5f")); dic.put(10, new JLabel("1.0f")); ctrlPanel.setLayout(new BoxLayout(ctrlPanel, BoxLayout.Y_AXIS)); JSlider sld1 = new JSlider(0, 10); sld1.addChangeListener(new SliderChangeLisner(btn1)); JSlider sld2 = new JSlider(0, 10); sld2.addChangeListener(new SliderChangeLisner(btn2)); JSlider sld3 = new JSlider(0, 10); sld3.addChangeListener(new SliderChangeLisner(btn3)); sld3.setLabelTable(dic); sld3.setPaintLabels(true); ctrlPanel.add(sld1); ctrlPanel.add(sld2); ctrlPanel.add(sld3); pane.add(ctrlPanel); frame.pack(); frame.setVisible(true); } class SliderChangeLisner implements ChangeListener { JButton btn = null; SliderChangeLisner(JButton btn) { this.btn = btn; } public void stateChanged(ChangeEvent e) { float val = (float)((JSlider)e.getSource()).getValue(); this.btn.setAlignmentX(val / 10.0f); this.btn.revalidate(); // 遅延自動レイアウト } } /** * CardLayout test * @see http://java.sun.com/docs/books/tutorial/uiswing/layout/card.html */ public void testCardLayout() { JFrame frame = new JFrame("CardLayout Test"); Container pane = frame.getContentPane(); final String BUTTON_CARD = "buttons"; final String TEXT_CARD = "texts"; final JPanel cards = new JPanel(new CardLayout()); JPanel ctrlPane = new JPanel(); String comboBoxItems[] = { BUTTON_CARD, TEXT_CARD }; JComboBox cb = new JComboBox(comboBoxItems); cb.addItemListener( new ItemListener() { public void itemStateChanged(ItemEvent e) { ((CardLayout)cards.getLayout()).show( cards, (String)e.getItem() ); } } ); cb.setEditable(false); ctrlPane.add(cb); JPanel card1 = new JPanel(); card1.add(new JButton("Button 1")); card1.add(new JButton("Button 2")); card1.add(new JButton("Button 3")); JPanel card2 = new JPanel(); card2.add(new JTextField("TextField", 10)); card2.add(new JTextField("TextField", 10)); card2.add(new JTextField("TextField", 10)); cards.add(card1, BUTTON_CARD); cards.add(card2, TEXT_CARD); pane.add(ctrlPane, BorderLayout.NORTH); pane.add(cards, BorderLayout.CENTER); frame.pack(); frame.setVisible(true); } /* ******* 以下メニュー用 ********* */ public LayoutTest() { setTitle("Layout Test"); JButton btn1 = new JButton("border"); btn1.addActionListener(new MyActionListener("border")); JButton btn2 = new JButton("box"); btn2.addActionListener(new MyActionListener("box")); JButton btn3 = new JButton("card"); btn3.addActionListener(new MyActionListener("card")); 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 ("border".equals(this.type)) { testBorderLayout(); } if ("box".equals(this.type)) { testBoxLayout(); } if ("card".equals(this.type)) { testCardLayout(); } } } 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() { LayoutTest lt = new LayoutTest(); lt.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); lt.setVisible(true); } } ); } }
© 2006 矢木浩人