Below your getContentPane()
place an element of the type JPanel
and define its type to CardLayout
, as in the figure below:
Your code looks something like this:
private void initialize() {
frmPrinc = new JFrame(); //variável de instancia
... //set size, bounds, title, etc
cards = new JPanel(new CardLayout()); //variável de instancia
frmPrinc.getContentPane().add(cards, BorderLayout.CENTER);
}
Create a JPanel
for each screen you want, and place them inside the cards
. In my case, I preferred to create several methods to create each screen, but you can also do it in a right way but different from mine. My code went like this:
private void telaInicial() {
telaInicial = new JPanel(); //variável de instancia
telaInicial.setLayout(new BorderLayout());
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 method add
is overloaded, and you have several options to use it, I chose to use a String to reference the object telaInicial
, but you could also use a whole.
Behold: Container (Java Platform SE 7)
Create as many screens as you want, to switch from one screen to another do so:
CardLayout cl = (CardLayout) (cards.getLayout());
cl.show(cards, "inicial"); //mudará para a tela inicial
Hello user4611, welcome to Stack Overflow. So that we can help you more objectively, please update your question with a brief, self-contained code example. Make it clear what you’ve tried and where you’re having problems; for example, in your case, a code with the
JFrame
and the event that should change thecontentPane
instantiatingJPanel
outsiders. Not knowing what you’ve done and where your difficulties are makes it very difficult to help you, as well as indirectly help other users who might have the same problem as you.– Anthony Accioly
You could post your code
JFrame
? So I can create aActionListener
that does what you asked, for you to use on your Button?– Paulo Roberto Rosa