import javax.swing.*; import java.awt.*; import java.awt.event.*; public class AutoCloseJFrame implements ActionListener { private static JFrame frame; public static void main(String[] args) { frame = new JFrame(); frame.setLayout(null); frame.setResizable(false); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(360, 180); frame.setTitle("Auto Close JFrame"); frame.getContentPane().setBackground(Color.BLACK); frame.setLocationRelativeTo(null); JButton button = new JButton("Click to Close JFrame"); button.setSize(180, 30); button.setLocation(90, 60); button.setFocusable(false); button.addActionListener(new AutoCloseJFrame()); button.setVisible(true); frame.add(button); frame.setVisible(true); } public void actionPerformed(ActionEvent event) { // "Dispose" of the frame (cause it to close) when the // button is clicked, which will cause the entire // program to end frame.dispose(); } }