📄 cardlayoutdemo.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CardLayoutDemo extends JFrame
{
private JPanel p1,p2,p3,p4,p5,pane;
private JLabel l1,l2,l3,l4,l5;
private CardLayout layout;
private JButton button;
public CardLayoutDemo()
{
super("CardLayout");
setSize(300,200);
try
{ //系统外观
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(Exception e){}
//获取内容面板
Container container = getContentPane();
pane = new JPanel();
layout = new CardLayout();
pane.setLayout(layout);
//创建面板
p1=new JPanel();
p2=new JPanel();
p3=new JPanel();
p4=new JPanel();
p5=new JPanel();
//创建标签
l1=new JLabel("这是第一个面板");
l2=new JLabel("这是第二个面板");
l3=new JLabel("这是第三个面板");
l4=new JLabel("这是第四个面板");
l5=new JLabel("这是第五个面板");
Font font = new Font("Serif", Font.PLAIN, 14);
l1.setFont(font);
l2.setFont(font);
l3.setFont(font);
l4.setFont(font);
l5.setFont(font);
//将标签添加到面板
p1.add(l1);
p2.add(l2);
p3.add(l3);
p4.add(l4);
p5.add(l5);
//设置面板背景颜色
p1.setBackground(Color.yellow);
p2.setBackground(Color.green);
p3.setBackground(Color.magenta);
p4.setBackground(Color.white);
p5.setBackground(Color.cyan);
pane.add("First",p1);
pane.add("Second",p2);
pane.add("Third",p3);
pane.add("Fourth",p4);
pane.add("Fifth",p5);
container.add(pane, BorderLayout.CENTER);
//创建按钮
button = new JButton("下一个面板");
button.setFont(font);
//注册监听器
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event)
{ layout.next(pane); }
});
container.add(button, BorderLayout.SOUTH);
layout.show(pane, "First");
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] arguments)
{
CardLayoutDemo ct=new CardLayoutDemo();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -