Remove JPanel from view

2

I would like to know how to remove a JPanel that is within a JFrame .

I do not want to use the information or anything, I just want it to close.

Using setVisible(false) leaves the panel invisible, I do not want this, I just want it to close.

I searched the internet but also did not get much success, how to do that?

JFrame code:

    public class MenuAultima extends JFrame { 
    public MenuAultima() {
        add(new Fundo());
        setTitle("A última esperança");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(800,625);
        setLocationRelativeTo(null);
        setResizable(false);
        setVisible(true);
    }
    public static void main (String[]args){
        new MenuAultima();
    }
}

JPanel code:

    public class Fundo extends JPanel  implements MouseListener{
    private Image fundo; 
    private Image novojogo;
    private Image comojogar;
    private Image  sair;
    public Fundo() {
        setFocusable(true);
        addMouseListener(this);
        ImageIcon Menu = new ImageIcon ("Imagens-menu\menu.fw.png");
        fundo = Menu.getImage();
        ImageIcon botao = new ImageIcon ("Imagens-menu\novojogo.fw.png");
        novojogo = botao.getImage();
        ImageIcon botaodois = new ImageIcon ("Imagens-menu\comojogar.fw.png");
        comojogar = botaodois.getImage();
        ImageIcon botaotres = new ImageIcon ("Imagens-menu\sair.fw.png");
        sair = botaotres.getImage();
    }
    public void paint (Graphics g){
        Graphics2D graficos = (Graphics2D) g;
        graficos.drawImage(fundo, 0, 0, null);
        graficos.drawImage(novojogo, 511, 346, null);
        graficos.drawImage(comojogar, 498, 410, null);
        graficos.drawImage(sair, 561 , 469 , null);
    }
    public Rectangle getBotaoComoJogar(){
        return new Rectangle(498, 410, comojogar.getWidth(null), comojogar.getHeight(null));
    }
    public String coords(MouseEvent e){
        return e.getX() + ", " + e.getY();
    }
    public void mouseClicked(MouseEvent e){
        Rectangle BotaoComoJogar = getBotaoComoJogar();
        if (BotaoComoJogar.contains(getMousePosition())){

            //Coisas acontecem aqui

        }
    }
    public void mousePressed(MouseEvent e){

    }
    public void mouseReleased(MouseEvent e){

    }
    public void mouseEntered(MouseEvent e){

    }
    public void mouseExited(MouseEvent e){

    }
}

asked by anonymous 31.03.2016 / 13:00

1 answer

2

Since your JPanel is a separate class, you will need to retrieve JFrame in it, so you can add the new panel and remove the current one. I do not consider this a good practice because I understand that we are delegating functions to the JPanel class that should be the responsibility of JFrame .

Anyway, adapting to your code, it would look like this:

public void mouseClicked(MouseEvent e){
    Rectangle BotaoComoJogar = getBotaoComoJogar();
    if (BotaoComoJogar.contains(getMousePosition())){
        JFrame janelaPrincipal = (JFrame) SwingUtilities.getWindowAncestor(this);
        janelaPrincipal.remove(this);
        janelaPrincipal.add(new OutroJPanel());
        janelaPrincipal.revalidade();
    }
}

The method SwingUtilities.getWindowAncestor() will retrieve the JFrame where you entered the panel (the panel should be passed as an argument, so I used this ). Note that if the panel is added to another, this line will give cast error, because as can be seen in the documentation, this method returns the window only if the passed component was added to one. If it is inside another panel, the method returns null.

The revalidade() method will notify the window of changes and should be redrawn.

    
31.03.2016 / 15:07