Problema com painéis! :(

salve galera!!!

Sou novo no fórum… estou no 4º período de Ciência da Computação na PUC Minas em Poços de Caldas.

Não tenho quase que nenhuma experiência com Java apesar de já ter conhecimentos sobre OO e tal…

to com um problema que é o seguinte:

basicamente o usuário seleciona uma opção através do RadioButton… caso selecione painel1 o painel1 será mostrado… caso selecione o painel2 o painel 2 será mostrado…

acontece que quando o painel 2 for selecionado preciso remover o painel 1 e entao exibir o painel 2… e vice versa!!!

existe um método para q eu possa remover o painel corrente???
isso seria tratado dentro do método actionperformed??

segue o código simples que tentei implementar com a idéia:

[code]import java.awt.Color;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.*;

public class testePaineis extends JFrame implements ActionListener{

JPanel painel1, painel2;
JButton botao1, botao2;
JLabel label1, label2;

public testePaineis(){
	
	getContentPane().setLayout(null);
	setBounds(0,0,750,450);
	
	painel1 = new JPanel();
	painel1.setBounds(300,100,200,100);
	painel1.setBackground(Color.blue);
	
	painel2 = new JPanel();
	painel2.setBounds(300,250,200,100);
	painel2.setBackground(Color.green);
	
	botao1 = new JButton("painel 1");
	botao1.setBounds(150,150,80,30);
	botao1.addActionListener(this);
	
	botao2 = new JButton("painel 2");
	botao2.setBounds(150,190,80,30);
	botao2.addActionListener(this);
	
	label1 = new JLabel("PAINEL 1");
	label1.setBounds(120,120,50,20);
	
	label2 = new JLabel("PAINEL 2");
	label2.setBounds(120,120,50,20);
	
	painel1.add(label1);
	painel2.add(label2);
	getContentPane().add(botao1);
	getContentPane().add(botao2);
	
}


public void actionPerformed(ActionEvent Evento)
{
	Object ObjetoRecebeuEvento;
	ObjetoRecebeuEvento = Evento.getSource();

    if (ObjetoRecebeuEvento == botao1){
    	//preciso remover o painel corrente para então adicionar o painel 1
    	getContentPane().add(painel1);
    	((JPanel) getContentPane()).updateUI();
    }
    
    if (ObjetoRecebeuEvento == botao2){
    	//preciso remover o painel corrente para então adicionar o painel 2
    	getContentPane().add(painel2);
    	((JPanel) getContentPane()).updateUI();
    	
    }

}

public static void main(String[] args) {
     testePaineis teste = new testePaineis();
     teste.setVisible(true);
}

}[/code]

quem puder ajudar agradeço!
abraço!!!

Segue um exemplo utilizando CardLayout, vê se a idéia é essa:

[code]import java.awt.;
import java.awt.event.
;
import javax.swing.*;

public class CardLayoutDemo implements ItemListener {
JPanel cards; //a panel that uses CardLayout
final static String BUTTONPANEL = “Card with JButtons”;
final static String TEXTPANEL = “Card with JTextField”;

public void addComponentToPane(Container pane) {
    //Put the JComboBox in a JPanel to get a nicer look.
    JPanel comboBoxPane = new JPanel(); //use FlowLayout
    String comboBoxItems[] = { BUTTONPANEL, TEXTPANEL };
    JComboBox cb = new JComboBox(comboBoxItems);
    cb.setEditable(false);
    cb.addItemListener(this);
    comboBoxPane.add(cb);
    
    //Create the "cards".
    JPanel card1 = new JPanel();
    card1.add(new JButton("Button 1"));
    card1.add(new JButton("Button 2"));
    card1.add(new JButton("Button 3"));
    
    JPanel card2 = new JPanel();
    card2.add(new JTextField("TextField", 20));
    
    //Create the panel that contains the "cards".
    cards = new JPanel(new CardLayout());
    cards.add(card1, BUTTONPANEL);
    cards.add(card2, TEXTPANEL);
    
    pane.add(comboBoxPane, BorderLayout.PAGE_START);
    pane.add(cards, BorderLayout.CENTER);
}

public void itemStateChanged(ItemEvent evt) {
    CardLayout cl = (CardLayout)(cards.getLayout());
    cl.show(cards, (String)evt.getItem());
}

/**
 * Create the GUI and show it.  For thread safety,
 * this method should be invoked from the
 * event dispatch thread.
 */
private static void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("CardLayoutDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    //Create and set up the content pane.
    CardLayoutDemo demo = new CardLayoutDemo();
    demo.addComponentToPane(frame.getContentPane());
    
    //Display the window.
    frame.pack();
    frame.setVisible(true);
}

public static void main(String[] args) {
    /* Use an appropriate Look and Feel */
    try {
        //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
    } catch (UnsupportedLookAndFeelException ex) {
        ex.printStackTrace();
    } catch (IllegalAccessException ex) {
        ex.printStackTrace();
    } catch (InstantiationException ex) {
        ex.printStackTrace();
    } catch (ClassNotFoundException ex) {
        ex.printStackTrace();
    }
    /* Turn off metal's use of bold fonts */
    UIManager.put("swing.boldMetal", Boolean.FALSE);
    
    //Schedule a job for the event dispatch thread:
    //creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });
}

}[/code]

T+

Usando um pouco de POG, dá pra “resetar” a JFrame toda vez que um botão é acionado. Mas eu não achei isso muito didático… talvez alguém consiga resolver de forma mais correta:

[code]public void actionPerformed(ActionEvent Evento)
{
Object ObjetoRecebeuEvento;
ObjetoRecebeuEvento = Evento.getSource();

    if (ObjetoRecebeuEvento == botao1){  
        //preciso remover o painel corrente para então adicionar o painel 1 
        getContentPane().removeAll();
        getContentPane().add(botao1);  
        getContentPane().add(botao2);  
        getContentPane().add(painel1);
        ((JPanel) getContentPane()).updateUI();  
    }  
      
    if (ObjetoRecebeuEvento == botao2){  
        //preciso remover o painel corrente para então adicionar o painel 2 
        getContentPane().removeAll();
        getContentPane().add(botao1);  
        getContentPane().add(botao2);   
        getContentPane().add(painel2);  
        ((JPanel) getContentPane()).updateUI();  
          
    }  
  
}[/code]