Toggle Jpanels within a single JFrame

2

In a Java code, I have a Jframe and inside this JFrame I have a contentPane (Jpanel). I have several screens in JPanel format, I want to swap those JPanels by clicking on a button that is docked inside each Jpanel. By clicking this button, the contentPane will be modified using and displaying another Jpanel screen.

    
asked by anonymous 04.02.2014 / 15:28

3 answers

3

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);}

CreateaJPanelforeachscreenyouwant,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
    
04.02.2014 / 17:02
1

Here's an example:

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class SeuFrame extends JFrame {

  private JPanel panel1;
  private JPanel panel2;
  private JButton botao1;
  private JButton botao2;

  public SeuFrame() {
    panel1 = new JPanel();
    panel2 = new JPanel();

    botao1 = new JButton( "Trocar para panel2" );
    botao1.setPreferredSize( new Dimension( 200, 30 ) );

    botao2 = new JButton( "Trocar para panel1" );
    botao2.setPreferredSize( new Dimension( 200, 30 ) );

    panel1.add( botao1 );
    panel2.add( botao2 );

    ActionListener actionListener = new ActionListener() {
      public void actionPerformed( ActionEvent e ) {
        JPanel panelTrocar = null;
        if ( e.getSource() == botao1 ) {
          panelTrocar = panel2;
        }
        else {
          panelTrocar = panel1;
        }
        getContentPane().removeAll();
        getContentPane().add( panelTrocar );
        revalidate();
        repaint();
      }
    };

    botao1.addActionListener( actionListener );
    botao2.addActionListener( actionListener );

    getContentPane().add( panel1 );
  }
}
    
04.02.2014 / 16:56
0

The CardLayout Layout is just right for you. You can use it to manage the JPanel you want to have active (visible).

You can see an example of how it works:

link

    
05.02.2014 / 13:18