Skip to content

Instantly share code, notes, and snippets.

@ijaureguialzo
Created May 20, 2021 19:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ijaureguialzo/4708720f0e49abd622d871a4651d3b6b to your computer and use it in GitHub Desktop.
Save ijaureguialzo/4708720f0e49abd622d871a4651d3b6b to your computer and use it in GitHub Desktop.
Cerrar un JFrame desde dentro de un JPanel
package com.jaureguialzo;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Ventana {
private JButton cerrarButton;
private JPanel panel;
public Ventana() {
cerrarButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFrame topFrame = (JFrame) SwingUtilities.getWindowAncestor(panel);
topFrame.dispose();
}
});
}
public static void main(String[] args) {
JFrame frame = new JFrame("Ventana");
frame.setContentPane(new Ventana().panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment