Java CardLayout() Constructor
Syntax
CardLayout() constructor from CardLayout has the following syntax.
public CardLayout()
Example
In the following code shows how to use CardLayout.CardLayout() constructor.
// w ww. j a v a2 s .c o m
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main {
public static void main(String[] args) {
JFrame aWindow = new JFrame();
aWindow.setSize(400, 400);
aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
aWindow.add(new CardLayoutPanel());
aWindow.setVisible(true);
}
}
class CardLayoutPanel extends JPanel implements ActionListener {
CardLayout card = new CardLayout();
public CardLayoutPanel() {
setLayout(card);
JButton button;
for (int i = 1; i <= 6; i++) {
add(button = new JButton("Press " + i), "Card" + i);
button.addActionListener(this);
}
}
public void actionPerformed(ActionEvent e) {
card.next(this);
}
}
Home »
Java Tutorial »
java.awt »
Java Tutorial »
java.awt »