📄 cardlayoutuse.java
字号:
package chapter11;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.CardLayout;
public class CardLayoutUse extends Frame {
public CardLayoutUse() {
String names[] = { "RED", "YELLOW", "GREEN" };
Color[] colors = { Color.RED, Color.YELLOW, Color.GREEN };
Button bu[] = new Button[3];
Panel pa[] = new Panel[3];
Panel topP = new Panel();
final Panel nextP = new Panel();
GridLayout gl = new GridLayout(1, 3);
final CardLayout cl = new CardLayout();
topP.setLayout(gl);
nextP.setLayout((LayoutManager) cl);
// nextP.setLayout(new CardLayout());
for (int i = 0; i < 3; i++) {
bu[i] = new Button(names[i]);
bu[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Button button = (Button) e.getSource();
cl.show(nextP, button.getLabel());
}
});
topP.add(bu[i]);
pa[i] = new Panel();
pa[i].setBackground(colors[i]);
nextP.add(pa[i], names[i]);
}
add(topP, BorderLayout.NORTH);
add(nextP, BorderLayout.CENTER);
setSize(300, 300);
setVisible(true);
}
public static void main(String[] args) {
new CardLayoutUse();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -