2
I would like to know how to remove one JPanel
that is inside a JFrame
.
I don’t want to use the information or anything, I just want it to close.
Use setVisible(false)
makes the panel invisible, I don’t want it, I just want it to close.
I looked on the internet but I also did not get much success, how to do this?
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();
}
}
Code of the Jpanel:
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){
}
}
Where there is the comment I would like to put an action where the Jpanel Background was closed and another Jpanel for example, how to play, was opened.
Add your jpanel code,
setvisible()
already meets what you want, there is no other way to hide a component, but the space it occupies will not disappear.– user28595
@Diegof This makes it invisible but does not close. It will bring problems in my code in the future. There is really no other way?
– Gregory Lewi
Isn’t there a . Dispose() function? Have you tried searching for something more targeted to it? Maybe it answers what you need.
– Leonardo Coelho
@Leonardocoelho Ispose is for top-level containers such as jframe, jpanel does not have Ispose.
– user28595
Gregory you can remove the component by using
seuFrame.remove(seuJPanel)
. Other than that, I don’t see any less laborious way to do what you want.– user28595
@Diegof Yes, I was trying to use this, but like this: There’s a button inside my Jpanel that should make it close and open another Jpanel, you know?
– Gregory Lewi
It would be interesting to add the code to the question and edit it, explaining better how your application works, what you want to do and what you can’t. It makes it even easier to answer.
– user28595
Edited. I explained exactly what I want to happen there
– Gregory Lewi