/*
* @(#) CardLayoutDemo.java
*/
import java.awt.*;
import javax.swing.*;
public class CardLayoutDemo extends JFrame {
private Button button1, button2, button3;
public CardLayoutDemo() {
button1 = new Button("Button1");
button2 = new Button("Button2");
button3 = new Button("Button3");
CardLayout cardLayout = new CardLayout(5, 10);
setLayout(cardLayout);
add(button1, "card1");
add(button2, "card2");
add(button3, "card3");
setSize(400, 300);
cardLayout.show(this, "card1");
setVisible(true);
}
public static void main(String[] args) {
CardLayoutDemo application = new CardLayoutDemo();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
程序在执行时出现异常:
Exception in thread "main" java.lang.IllegalArgumentException: wrong parent for
CardLayout
at java.awt.CardLayout.checkLayout(CardLayout.java:383)
at java.awt.CardLayout.show(CardLayout.java:505)
at CardLayoutDemo.<init>(CardLayoutDemo.java:23)
at CardLayoutDemo.main(CardLayoutDemo.java:28)
这个程序中cardLayout对象的父对象应该是谁啊?
----------------解决方案--------------------------------------------------------
不是setlayout么
----------------解决方案--------------------------------------------------------
仔细看看API吧
----------------解决方案--------------------------------------------------------