CardLayout布局
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Demo extends JFrame {
private JPanel pane = null;
private JPanel p = null;
private CardLayout card = null;
private JButton button_1=null;
private JButton button_2=null;
private JButton b_1=null,b_2=null,b_3=null,b_4=null;
private JPanel p_1=null,p_2=null,p_3=null;
public Demo(){
card = new CardLayout(10,15);
pane = new JPanel(card);
p = new JPanel();
button_1 = new JButton(“< 上一步”);
button_2 = new JButton(“下一步 >”);
b_1 = new JButton(“1”);
b_2 = new JButton(“2”);
b_3 = new JButton(“3”);
b_4 = new JButton(“4”);
/*
*
*setMargin
public void setMargin(Insets m)设置按钮边框和标签之间的空白。将该空白设置为 null 会造成按钮使用默认空白。
按钮的默认 Border 对象将使用该值来创建适当的空白。不过,如果在按钮上设置非默认边框,则由 Border 对象负责创建适当的空白(否则此属性将被忽略)。
参数:
m - 边框和标签之间的间隔
Insets(int top, int left, int bottom, int right)
创建并初始化具有指定顶部、左边、底部、右边 inset 的新 Insets 对象。
getMargin
public Insets getMargin()返回按钮边框和标签之间的空白。
返回:
一个指定按钮边框和标签之间的空白的 Insets 对象
b_1.setMargin(new Insets(2,2,2,2));
b_2.setMargin(new Insets(2,2,2,2));
b_3.setMargin(new Insets(2,2,2,2));
b_4.setMargin(new Insets(15,5,15,5));
*/
p.add(button_1);
p.add(b_1);
p.add(b_2);
p.add(b_3);
p.add(b_4);
p.add(button_2);
p_1 = new JPanel();
p_2 = new JPanel();
p_3 = new JPanel();
p_1.setBackground(Color.RED);
p_2.setBackground(Color.BLUE);
p_3.setBackground(Color.GREEN);
p_1.add(new JLabel("JPanel_1"));
p_2.add(new JLabel("JPanel_2"));
p_3.add(new JLabel("JPanel_3"));
pane.add(p_1,"p1");
pane.add(p_2,"p2");
pane.add(p_3,"p3");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
card.previous(pane);
}
});
button_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
card.next(pane);
}
});
b_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
card.show(pane,"p1");
}
});
b_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
card.show(pane,"p2");
}
});
b_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
card.show(pane,"p3");
}
});
b_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
card.first(pane);
}
});
this.getContentPane().add(pane);
this.getContentPane().add(p,BorderLayout.EAST);
this.setSize(300, 200);
this.setVisible(true);
pack();
}
public static void main(String[] args) {
new Demo();
}
}