CardLayoutTest.java
Dosyayı İndir
package com.godoro.samples.awt;
import java.awt.*;
import java.awt.event.*;
public class CardLayoutTest
extends Frame
implements ActionListener
{
private CardLayout cardLayout;
private Panel centerPanel;
public CardLayoutTest()
{
setBounds(10, 10, 400, 400);
Panel buttonPanel = new Panel();
Button button1 = new Button("Show Card 1");
button1.addActionListener(this);
buttonPanel.add(button1);
Button button2 = new Button("Show Card 2");
button2.addActionListener(this);
buttonPanel.add(button2);
centerPanel = new Panel();
cardLayout = new CardLayout();
centerPanel.setLayout(cardLayout);
Label label1 = new Label("Card 1");
label1.setBackground(Color.RED);
centerPanel.add(label1, "label1");
Label label2 = new Label("Card 2");
label2.setBackground(Color.BLUE);
centerPanel.add(label2, "label2");
setLayout(new BorderLayout());
add(buttonPanel, BorderLayout.NORTH);
add(centerPanel, BorderLayout.CENTER);
}
@Override
public void actionPerformed(ActionEvent e)
{
String command = e.getActionCommand();
if (command.equals("Show Card 1")) {
cardLayout.show(centerPanel, "label1");
} else if (command.equals("Show Card 2")) {
cardLayout.show(centerPanel, "label2");
}
}
public static void main(String[] args)
{
CardLayoutTest test = new CardLayoutTest();
test.setVisible(true);
}
}
Dosyayı İndir