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

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 developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.