Created
May 20, 2021 19:56
-
-
Save ijaureguialzo/4708720f0e49abd622d871a4651d3b6b to your computer and use it in GitHub Desktop.
Cerrar un JFrame desde dentro de un JPanel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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