【Java开源代码栏目提醒】:网学会员,鉴于大家对Java开源代码十分关注,论文会员在此为大家搜集整理了“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();
}
}
上一篇:
CardLayoutTest.java
下一篇:
“群众路线”:内涵、逻辑与特质心得体会