Close window when clicking JButton

1

Is it possible to click on a button and JFrame to be closed as if I had clicked on the X of the window? If it is possible, how do I create it?

What I've already studied is using ActionListener but I can not find the code to close the window.

    
asked by anonymous 14.11.2017 / 01:37

1 answer

3

It is possible, yes, you define a listener for the button and inside it, it invokes the dispose() " of the instance of JFrame you want to close within this listener:

botao.addActionListener(e -> seuFrame.dispose());
    
14.11.2017 / 01:42