im on that stage of Java learning where you get to make gui’s, for this program, im using CardLayout for swapping between panels, the thing is, i get this error over and over, i’ve searched everywhere and i still cannot solve it, the error as shown on console is:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: class java.awt.FlowLayout cannot be cast to class java.awt.CardLayout (java.awt.FlowLayout and java.awt.CardLayout are in module java.desktop of loader ‘bootstrap’)
The console said my problem is on this part of the code:
public FrameMenuPrincipal() {
initComponents();
deffbg = new JPanel();
deffbg.add(pane1, "panewelc");
deffbg.add(pane2, idcreate);
deffbg.add(pane3, idsite);
cl = (CardLayout)(deffbg.getLayout());
cl.show(deffbg, idmenu);
}
deffbg being the parent panel;
For adding more context:
I’ve been using this tutorial
And I’m programming on NetBeans, using the Gui builder
Also, sorry if this question is badly made, i’m new to stack overflow and im also Hispanic so, sorry if there’s any grammar errors here
>Solution :
By default, JPanel used a FlowLayout. If you want to use something different, you can provide it in the constructor. So in this case, if you wanted to use a CardLayout you could instantiate it like this:
deffbg = new JPanel(new CardLayout());