JAVAレイアウトマネージャ
3189 ワード
import java.awt.Color;
import java.awt.*;
import javax.swing.*;
public class JustCode {
public static class IntroPanel extends JPanel{
public IntroPanel(){
setBackground (Color.green);
JLabel l1 = new JLabel ("Layout Manger Demonstration");
JLabel l2 = new JLabel ("Choose a tab to see an example of a layout manager.");
add(l1);
add(l2);
}
}
//
public static class FlowPanel extends JPanel{
public FlowPanel(){
setLayout (new FlowLayout());
setBackground (Color.green);
JButton b1 = new JButton ("BUTTON 1");
JButton b2 = new JButton ("BUTTON 2");
JButton b3 = new JButton ("BUTTON 3");
JButton b4 = new JButton ("BUTTON 4");
JButton b5 = new JButton ("BUTTON 5");
add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
}
}
//
public static class BorderPanel extends JPanel{
public BorderPanel(){
setLayout(new BorderLayout());
setBackground (Color.green);
JButton b1 = new JButton ("BUTTON 1");
JButton b2 = new JButton ("BUTTON 2");
JButton b3 = new JButton ("BUTTON 3");
JButton b4 = new JButton ("BUTTON 4");
JButton b5 = new JButton ("BUTTON 5");
add(b1,BorderLayout.CENTER);
add(b2,BorderLayout.NORTH);
add(b3,BorderLayout.SOUTH);
add(b4,BorderLayout.EAST);
add(b5,BorderLayout.WEST);
}
}
//
public static class GridPanel extends JPanel{
public GridPanel(){
setLayout (new GridLayout(2,3));
setBackground(Color.green);
JButton b1 = new JButton ("BUTTON 1");
JButton b2 = new JButton ("BUTTON 2");
JButton b3 = new JButton ("BUTTON 3");
JButton b4 = new JButton ("BUTTON 4");
JButton b5 = new JButton ("BUTTON 5");
add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
}
}
//
public static class BoxPanel extends JPanel{
public BoxPanel(){
setLayout( new BoxLayout (this, BoxLayout.Y_AXIS));
setBackground (Color.green);
JButton b1 = new JButton ("BUTTON 1");
JButton b2 = new JButton ("BUTTON 2");
JButton b3 = new JButton ("BUTTON 3");
JButton b4 = new JButton ("BUTTON 4");
JButton b5 = new JButton ("BUTTON 5");
add(b1);
add(Box.createRigidArea(new Dimension (0, 10)));
add(b2);
add(Box.createHorizontalGlue());
add(b3);
add(b4);
add(Box.createRigidArea(new Dimension (0, 20)));
add(b5);
}
}
public static void main(String args[])
{
JFrame frame = new JFrame ("Layout Manager Demo");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
JTabbedPane tp = new JTabbedPane();
tp.addTab("Intro", new IntroPanel());
tp.addTab("Flow", new FlowPanel());
tp.addTab("Border", new BorderPanel());
tp.addTab("Grid", new GridPanel());
tp.addTab("Box", new BoxPanel());
frame.getContentPane().add(tp);
frame.pack();
frame.show();
}
}