CardLayout布局管理器允许向一个容器中添加多个组件,允许在同一位置添加并显示所有组件。不过任意时刻只有一个组件是可见的,通过调用 CardLayout中定义的first()、last()、next()和previous()方法指定显示哪个组件,显示顺序正如这些方法名一样。另 外还有show()方法用于指定显示某个组件,这时将不考虑容器中该组件与其他组件的关系。
CardLayout类可以说是Java中作用最小的布局管理器,用这个管理器,可以使得容器象一个卡片盒,而容器中的页面象卡片盒中的卡片一样任意翻动 显示。引入Swing之前,CardLayout用于创建选项卡用户界面,但Swing中的JTabbedPane提供了更好的实现机制,不过某些情况还 是会用到CardLayout,如制作Windows 风格的用户向导界面来依次显示一系列面板。
CardLayout布局管理器有以下构造函数:
CardLayout():创建一卡片布局管理器
CardLayout(int hgap, int vgap):创建一卡片布局管理器,并指定左右边距和上下边距。
下面用CardLayout写了一个小例子:
(注意测试环境是JDK1.6,其中透明属性是1.6之后才有,所以低于1.6不能运行,而使用的透明方法在JDK1.7中对此方法做了修改,所以1.7也会运行错误 ,详见 )
主要用到BorderLay,FlowLayout来布局按钮和面板,图片采用的是CardLayout,然后几个按钮的监听事件也是调用CardLayout布局管理器中的一些方法。
源码:
package osc.test.cardlayout;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
public class CardTest extends JFrame {
public static final Image LOG = new ImageIcon(
CardTest.class.getResource("/images/LOG.png")).getImage();;
private CardLayout cardLayout;
private JPanel imagePanel;
private ImageLabel[] imageLabels;
private JPanel buttonPanel;
private JButton firstButton;
private JButton previousButton;
private JButton nextButton;
private JButton lasButton;
public CardTest() {
super("CardLayout Test --by Alog2012");
init();
}
private void init() {
Container container = getContentPane();
container.add(getImagePanel(), BorderLayout.CENTER);
container.add(getButtonPanel(), BorderLayout.SOUTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
com.sun.awt.AWTUtilities.setWindowOpacity(this, 0.95f);
pack();
// 获取系统工具箱
Toolkit kit = Toolkit.getDefaultToolkit();
int scrW = kit.getScreenSize().width;
int scrH = kit.getScreenSize().height;
int locale_x = (scrW - getSize().width) / 2;
int locale_y = (scrH - getSize().height) / 2;
super.setLocation(locale_x, locale_y);
super.setIconImage(LOG);
setVisible(true);
}
protected JPanel getImagePanel() {
imagePanel = new JPanel(cardLayout = new CardLayout());
setMinimumSize(new Dimension(240, 200));
setPreferredSize(new Dimension(540, 450));
setMaximumSize(new Dimension(720, 640));
imageLabels = getImageLabels();
for (int i = 0; i < 8; i++) {
imagePanel.add(imageLabels[i], String.valueOf(i));
}
return imagePanel;
}
protected ImageLabel[] getImageLabels() {
imageLabels = new ImageLabel[8];
for (int i = 0; i < 8; i++) {
Image image = new ImageIcon(
CardTest.class.getResource("/images/pic_" + i + ".jpg"))
.getImage();
imageLabels[i] = new ImageLabel(image);
}
return imageLabels;
}
protected JPanel getButtonPanel() {
buttonPanel = new JPanel();
buttonPanel.add(getFirstButton());
buttonPanel.add(getPreviousButton());
buttonPanel.add(getNextButton());
buttonPanel.add(getLasButton());
return buttonPanel;
}
protected JButton getFirstButton() {
firstButton = new JButton("首页");
firstButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cardLayout.first(imagePanel);
}
});
return firstButton;
}
protected JButton getPreviousButton() {
previousButton = new JButton("向前");
previousButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cardLayout.previous(imagePanel);
}
});
return previousButton;
}
protected JButton getNextButton() {
nextButton = new JButton("向后");
nextButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cardLayout.next(imagePanel);
}
});
return nextButton;
}
protected JButton getLasButton() {
lasButton = new JButton("末页");
lasButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cardLayout.last(imagePanel);
}
});
return lasButton;
}
class ImageLabel extends JLabel {
private Image image;
public ImageLabel(Image image) {
this.image = image;
}
protected void paintComponent(Graphics g) {
g.drawImage(image, 0, 0, getSize().width, getSize().height, this);
}
}
public static void main(String[] args) {
try {
UIManager
.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception e) {
e.printStackTrace();
}
new CardTest();
}
}
参考:
1.《 Pro Java Programming(Second Edition) 》
2.《 Core Java I 》