please help me how to close the window.
code:

    JButton quit = new JButton();

    public Mainmenu() {
        setLayout(new FlowLayout());

        Icon quitb = new ImageIcon(getClass().getResource("quit.jpg"));
        quit = new JButton(quitb);
        add(quit);

        quit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

            }
        });

Recommended Answers

To close a JFrame and release all its resources you can call its dispose() method.

Jump to Post

All 2 Replies

To close a JFrame and release all its resources you can call its dispose() method.

depends on....

  • if current parent JFrame/JDialog/JWindow for JButton quit = new JButton(); or not (not possible to suggest something based on your code and description),

  • if yes, then don't extend JFrame/JDialog/JWindow, create this Object as local variable, use this variable e.g. myWindow.setVisible(false) btw equals for JVM resources setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE/HIDE_ON_CLOSE) at runtime, you can to re_use this window for another action (myWindow.setVisible(true))

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.