2013年2月14日 星期四

Layout - BoxLayout


BoxLayout

BoxLayout1, 水平與垂直Box, X_AXIS, Y_AXIS
BoxLayout2, 自動推開元件: Glue, HorizontalGlue, VerticalGlue
BoxLayout3, 固定大小的分隔: HorizontalStrut, VerticalStrut
BoxLayout4, 有長跟寬的分隔: RigidArea
BoxLayout5, 有最小最大跟預設大小的RigidArea: Box.Filler
BoxLayout6, Box中在裝Box: HorizontalBox, VerticalBox

   X_AXIS:从左到右水平布置组件。
   Y_AXIS:从上到下垂直布置组件。
   LINE_AXIS:根据容器的 ComponentOrientation 属性,按照文字在一行中的排列方式布置组件。如果容器的 ComponentOrientation 表示水平,则将组件水平放置,否则将它们垂直放置。对于水平方向,如果容器的 ComponentOrientation 表示从左到右,则组件从左到右放置,否则将它们从右到左放置。对于垂直方向,组件总是从上到下放置的。
   PAGE_AXIS:根据容器的 ComponentOrientation 属性,按照文本行在一页中的排列方式布置组件。如果容器的 ComponentOrientation 表示水平,则将组件垂直放置,否则将它们水平放置。对于水平方向,如果容器的 ComponentOrientation 表示从左到右,则组件从左到右放置,否则将它们从右到左放置。对于垂直方向,组件总是从上向下放置的。

BoxLayout1, 水平與垂直Box, X_AXIS, Y_AXIS


import java.awt.Container;
import java.awt.GridLayout;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class TestBoxLayout1 {
public static void main(String[] args) {
JFrame frame = new JFrame("BoxLayout-1");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container cp = frame.getContentPane();
cp.setLayout(new GridLayout(0, 1));

JPanel panel = null;
panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
panel.add(new JButton("1"));
panel.add(new JButton("2"));
panel.add(new JButton("3"));
cp.add(panel);

panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(new JButton("4"));
panel.add(new JButton("5"));
panel.add(new JButton("6"));
cp.add(panel);

frame.setVisible(true);
frame.pack();
}
}

BoxLayout2, 自動推開元件: Glue, HorizontalGlue, VerticalGlue

import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class TestBoxLayout2 {
public static void main(String[] args) {
JFrame frame = new JFrame("BoxLayout-2");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container cp = frame.getContentPane();
cp.setLayout(new GridLayout(0, 1));
JPanel panel = null;

// glue在垂列與水平的boxLayout中有作用
panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
panel.add(new JButton("1"));
panel.add(Box.createGlue());
panel.add(new JButton("2"));
panel.add(new JButton("3"));
cp.add(panel);

panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(new JButton("1"));
panel.add(Box.createGlue());
panel.add(new JButton("2"));
panel.add(new JButton("3"));
cp.add(panel);

// 水平的glue只在水平的BoxLayout中有作用
panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
panel.add(new JButton("1"));
panel.add(Box.createHorizontalGlue());
panel.add(new JButton("2"));
panel.add(new JButton("3"));
cp.add(panel);
// 水平的glue在垂列的box中沒有作用
panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(new JButton("1"));
panel.add(Box.createHorizontalGlue());
panel.add(new JButton("2"));
panel.add(new JButton("3"));
cp.add(panel);

frame.setVisible(true);
frame.pack();
}
}



BoxLayout3, 固定大小的分隔: HorizontalStrut, VerticalStrut

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class TestBoxLayout3 {
public static void main(String[] args) {
JFrame frame = new JFrame("BoxLayout-3");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container cp = frame.getContentPane();
cp.setLayout(new GridLayout(0, 1));
JPanel panel = null;

panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
panel.add(new JButton("1"));
panel.add(Box.createHorizontalGlue());
panel.add(new JButton("2"));
panel.add(new JButton("3"));
cp.add(panel);

// Strut可以固定分隔的大小
panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
panel.add(new JButton("1"));
panel.add(Box.createHorizontalStrut(50));
panel.add(new JButton("2"));
panel.add(new JButton("3"));
cp.add(panel);

frame.setVisible(true);
frame.pack();
}
}




BoxLayout4, 有長跟寬的分隔: RigidArea

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class TestBoxLayout4 {
public static void main(String[] args) {
JFrame frame = new JFrame("BoxLayout-4");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container cp = frame.getContentPane();
cp.setLayout(new GridLayout(0, 1));
JPanel panel = null;
// RigidArea就像是有寬跟高的strut
// 設置比較大的值 會讓排版的大小也變得更大
panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
panel.add(new JButton("1"));
panel.add(Box.createRigidArea(new Dimension(100, 50)));
panel.add(new JButton("2"));
panel.add(new JButton("3"));
cp.add(panel);
panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(new JButton("1"));
panel.add(Box.createRigidArea(new Dimension(100, 50)));
panel.add(new JButton("2"));
panel.add(new JButton("3"));
cp.add(panel);
frame.setVisible(true);
frame.pack();
}
}


BoxLayout5, 有最小最大跟預設大小的RigidArea: Box.Filler

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.Box;
import javax.swing.Box.Filler;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class TestBoxLayout5 {
public static void main(String[] args) {
JFrame frame = new JFrame("BoxLayout-5");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container cp = frame.getContentPane();
cp.setLayout(new GridLayout(0, 1));
JPanel panel = null;
//Filler 就像是可變動大小的Strut
panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
panel.add(new JButton("1"));
Dimension min = new Dimension(50, 50);
Dimension max = new Dimension(150,150);
Dimension preferred = new Dimension(100, 100);
Filler filler = new Box.Filler(min, preferred, max);
panel.add(filler);
panel.add(new JButton("2"));
panel.add(new JButton("3"));
cp.add(panel);
frame.setVisible(true);
frame.pack();
}
}



BoxLayout6, Box中在裝Box: HorizontalBox, VerticalBox

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;



public class TestBoxLayout6 {
public static void main(String[] args) {
JFrame frame = new JFrame("BoxLayout-6");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container cp = frame.getContentPane();
cp.setLayout(new GridLayout(0, 1));
JPanel panel = null;
// Box的功能 其實可以直接其他layout搭配Box來用!!!
panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
panel.add(new JButton("1"));
Box box = Box.createVerticalBox();
box.add(new JButton("4"));
box.add(new JButton("5"));
box.add(new JButton("6"));
panel.add(box);
panel.add(new JButton("2"));
box = Box.createVerticalBox();
box.add(new JButton("7"));
box.add(new JButton("8"));
box.add(new JButton("9"));
panel.add(box);
panel.add(new JButton("3"));
cp.add(panel);
frame.setVisible(true);
frame.pack();
}
}



沒有留言:

張貼留言