| ページ一覧 | ブログ | twitter |  書式 | 書式(表) |

MyMemoWiki

Swing レイアウト 1

提供: MyMemoWiki
2020年2月16日 (日) 04:32時点におけるPiroto (トーク | 投稿記録)による版
(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)
ナビゲーションに移動 検索に移動

レイアウト 1

Swing |

  • BorderLayout
  • BoxLayout
  • CardLayout

BorderLayout

1194 border.jpg

BoxLayout

1195 box.jpg

CardLayout

1枚目

1196 card01.jpg

2枚目

1197 card02.jpg

ソースコード

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);
        }
      }
    );
  }
}