📄 cardlayoutdemo.java
字号:
package chapter6.layout;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class CardLayoutDemo extends JFrame implements ActionListener {
boolean one = true;
JPanel p3;
public CardLayoutDemo() {
JPanel p1, p2, p4;
Container contentPane = this.getContentPane();
contentPane.setLayout(new GridLayout(2, 1));
p1 = new JPanel(); //card1
p1.setLayout(new GridLayout(1, 1));
p1.add(new Button("one"));
p2 = new JPanel(); //card2
p2.setLayout(new GridLayout(1, 1));
p2.add(new Button("two"));
p3 = new JPanel(new CardLayout()); //放置前面的两个panel
p3.add("one", p1); //one 为p1的标识符
p3.add("two", p2);
((CardLayout) p3.getLayout()).show(p3, "one"); //设置默认显示的卡片
contentPane.add(p3);
p4 = new JPanel();
JButton jButton1 = new JButton("Next");
jButton1.addActionListener(this);
p4.add(jButton1);
contentPane.add(p4);
this.setTitle("CardLayoutDemo");
this.setSize(400, 200);
this.setLocation(400, 400);
this.setVisible(true);
}
public void actionPerformed(ActionEvent event) {
if (one == true) {
((CardLayout) p3.getLayout()).show(p3, "two");
one = false;
} else {
((CardLayout) p3.getLayout()).show(p3, "one");
one = true;
}
}
public static void main(String[] args) {
CardLayoutDemo test = new CardLayoutDemo();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -