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