CardLayout usage
This code:
package test;
import java.awt.CardLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
publicclass Main{
privatestaticvoid createAndShowGUI(){
JFrame frame =new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new CardLayout());
frame.add(new JLabel("Label"));
frame.pack();
frame.setVisible(true);
}
publicstaticvoid main(String[] args){
javax.swing.SwingUtilities.invokeLater(new Runnable(){
publicvoid run(){
createAndShowGUI();
}
});
}
}
crashes with :
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: cannot add to layout: constraint must be a string
at java.awt.CardLayout.addLayoutComponent(CardLayout.java:190)
at java.awt.Container.addImpl(Container.java:1068)
at java.awt.Container.add(Container.java:935)
at javax.swing.JFrame.addImpl(JFrame.java:545)
at java.awt.Container.add(Container.java:352)
at test.Main.createAndShowGUI(Main.java:27)
................................................
With the line
frame.add(new JLabel("Label"));
changed to
frame.add(new JLabel("Label"),"");
everything works OK.
Am I always supposed to provide string identifiers for components while using CardLayout or is it a bug?
I'm using jdk 1.6.0 on WinXP.

