/**
* 卡片布局管理器
*
* @time 9:49:57 AM
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public class Test_CardLayuot extends Frame {
private static final long serialVersionUID = 1L;
// 命令面板
Panel pnlCommandArea = new Panel();
// 显示面板
Panel pnlDisplayArea = new Panel();
// 显示面板布局
CardLayout cardLayout = new CardLayout();
//
Button btnFirst = new Button("第一个");
Button btnPrevious = new Button("上一个");
Button btnNext = new Button("下一个");
Button btnLast = new Button("最后一个");
/**
* 构造器
*/
@SuppressWarnings("unused")
public Test_CardLayuot() {
btnFirst.setBackground(Color.white);
btnPrevious.setBackground(Color.white);
btnNext.setBackground(Color.white);
btnLast.setBackground(Color.white);
this.setLayout(new BorderLayout());
// 添加两个布局
this.add(pnlCommandArea, BorderLayout.NORTH);
this.add(pnlDisplayArea, BorderLayout.CENTER);
// 显示面板添加布局
pnlDisplayArea.setLayout(cardLayout);
// 四个显示面板
Panel firstPanel = new Panel();
// 背景色
firstPanel.setBackground(Color.yellow);
// 前景色
firstPanel.setForeground(Color.blue);
pnlDisplayArea.add("first", firstPanel);
firstPanel.add(new Label("这是第一张卡片"));
Panel secondPanel = new Panel();
secondPanel.setBackground(Color.pink);
secondPanel.setForeground(Color.blue);
pnlDisplayArea.add("first", secondPanel);
secondPanel.add(new Label("这是第二张卡片"));
Panel thirdPanel = new Panel();
thirdPanel.setBackground(Color.orange);
thirdPanel.setForeground(Color.blue);
pnlDisplayArea.add("first", thirdPanel);
thirdPanel.add(new Label("这是第三张卡片"));
Panel fourthPanel = new Panel();
fourthPanel.setBackground(Color.green);
fourthPanel.setForeground(Color.blue);
pnlDisplayArea.add("first", fourthPanel);
fourthPanel.add(new Label("这是第四张卡片"));
// 按钮添加监听
btnFirst.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
processAction(e);
}
});
btnPrevious.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
processAction(e);
}
});
btnNext.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
processAction(e);
}
});
btnLast.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
processAction(e);
}
});
// 按钮添加以面板
pnlCommandArea.add(btnFirst);
pnlCommandArea.add(btnPrevious);
pnlCommandArea.add(btnNext);
pnlCommandArea.add(btnLast);
}
@SuppressWarnings("deprecation")
public static void main(String[] args) {
Test_CardLayuot cardLayuot = new Test_CardLayuot();
cardLayuot.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
// 自适应控件大小
cardLayuot.pack();
cardLayuot.show();
}
/**
* 默认大小
*/
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
/**
* 单击后执行方法
*/
private void processAction(ActionEvent e) {
Button btnEvent = (Button) e.getSource();
if (btnEvent.equals(btnFirst)) {
cardLayout.first(pnlDisplayArea);
} else if (btnEvent.equals(btnLast)) {
cardLayout.last(pnlDisplayArea);
} else if (btnEvent.equals(btnPrevious)) {
cardLayout.previous(pnlDisplayArea);
} else if (btnEvent.equals(btnNext)) {
cardLayout.next(pnlDisplayArea);
}
}
}
swing学习笔记十一(卡片布局管理器CardLayout )
最新推荐文章于 2023-09-22 19:00:00 发布