Here is my code - i want b1 to close the window when i press it

public class Main {
    public static void main(String[] args) {
        javax.swing.JFrame w1 = new javax.swing.JFrame();
        javax.swing.JButton b1 = new javax.swing.JButton();
        w1.setBounds(0,0,300,300);

        java.awt.Container c = w1.getContentPane();
        c.setLayout(null);

        b1.setText("Exit");
        b1.setBounds(100, 100, 100, 30);
        c.add(b1);
        w1.setVisible(true);




    }
}

please insert the code needed to close the window when the exit button is clicked!

thank you so much

Append this in your code in main function only

b1.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e)
            {
               System.exit(0);
            }
        });

And always use code tags while posting code

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.