Ola Pessoal,
Alguém poderia dar umas dicas ou quem sabe até exemplos
de como eu posso mudar os JPanels de um Frame.
Ja li sobre CardLayout …
mas não to entendendo como eu devo fazer, o que eu devo ter …
Estou tentando algo apenas pra teste, esta assim:
public class PanelPrincipal extends JPanel {
JLabel label;
public PanelPrincipal() {
label = new JLabel("Panel Principal");
this.add(label);
}
}
public class PanelSecundario extends JPanel {
JLabel label;
public PanelSecundario() {
label = new JLabel("Panel Secundario");
this.add(label);
}
}
public class FramePrincipal extends JFrame {
private PanelPrincipal panelPrincipal;
private PanelSecundario panelSecundario;
private JButton button;
public FramePrincipal() {
panelPrincipal = new PanelPrincipal();
this.getContentPane().add(panelPrincipal, BorderLayout.CENTER);
button = new JButton("Mudar Panel");
button.addActionListener(new AcaoMudarPanel());
this.getContentPane().add(button, BorderLayout.SOUTH);
}
public void trocaPanel() {
this.remove(panelPrincipal);
this.repaint();
panelSecundario = new PanelSecundario();
this.getContentPane().add(panelSecundario, BorderLayout.CENTER);
this.repaint();
}
class AcaoMudarPanel implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
trocaPanel();
}
}
}
public class TesteInterface {
public static void main(String[] args) {
FramePrincipal framePrincipal = new FramePrincipal();
framePrincipal.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
framePrincipal.setSize(300, 300);
framePrincipal.setVisible(true);
}
}
Mas tambem não funciona …
Alguem sabe o que há de errado , ou se estou agindo certo desta forma?
Obrigado!