Under your getContentPane()
put an element of type JPanel
and set its type to CardLayout
, as in the figure below:
Yourcodelookssomethinglikethis:
privatevoidinitialize(){frmPrinc=newJFrame();//variáveldeinstancia...//setsize,bounds,title,etccards=newJPanel(newCardLayout());//variáveldeinstanciafrmPrinc.getContentPane().add(cards,BorderLayout.CENTER);}
CreateaJPanel
foreachscreenyouwant,andplacethemwithincards
.Inmycase,Ipreferredtocreateseveralmethodstocreateeachscreen,butyoucanalsodoitinadifferentwaybutdifferentfrommine.Mycodelookslikethis:
privatevoidtelaInicial(){telaInicial=newJPanel();//variáveldeinstanciatelaInicial.setLayout(newBorderLayout());cards.add(telaInicial,"inicial"); //"inicial" é a chave a ser usada para chamar o objeto "telaInicial"
... //adiciona todos os elementos necessários no JPanel telaInicial
}
The add
method is overloaded, and you have several options to use it, I have chosen to use a String to reference the object telaInicial
, but you could also use an integer.
See: Container (Java Platform SE 7)
Create as many screens as you like, to switch from one screen to another, do the following:
CardLayout cl = (CardLayout) (cards.getLayout());
cl.show(cards, "inicial"); //mudará para a tela inicial