12

I am creating a small application using AWT. When I try to close the window, the "close" button doesn't work.

Here's my code:

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;

class ButtonDemo1 implements ActionListener {
    Button b1;
    TextField tf;
    Frame f;

    ButtonDemo1(String s) {
        f = new Frame(s);
        b1 = new Button("OK");

        tf = new TextField(10);
        f.setSize(200, 250);
        f.setVisible(true);
        b1.addActionListener(this);

        f.add(tf);
        f.add(b1);

        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent we) {
                System.exit(0);
            }
        });

        f.setLayout(new FlowLayout());
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == b1) {
            tf.setText("Press Ok");
        }

    }

    public static void main(String args[]) {
        new ButtonDemo1("First");
    }
}

How can I fix the "close" button?

grooveplex
  • 2,492
  • 4
  • 28
  • 30
Ankur jain
  • 963
  • 3
  • 14
  • 21
  • 1
    Try putting f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); in your program. – Umesh K Mar 12 '11 at 07:32
  • 1
    @Umesh, please make your comment an answer so we can up vote you for rep. After all, this is the correct answer. – krock Mar 12 '11 at 07:42
  • 6
    @krock: Is it? Even though he's not using a JFrame and so that method is not available to his frame? – Mark Peters Mar 12 '11 at 08:07
  • 1
    @user: your code works fine for me. Clicking on the close window icon terminates the program. If there's a problem, I suspect it's not in the code you are sharing. – Mark Peters Mar 12 '11 at 08:11
  • Why does the original source import java.applet classes? System.exit(n) cannot be called in an applet, or any frame launched from an applet. – Andrew Thompson Mar 12 '11 at 09:09

3 Answers3

17

It's better to use the method public void dispose()

Why should you have to dispose() a java.awt.Window that goes out of scope?

f.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent we) {
            dispose();
         }
     }
);
Community
  • 1
  • 1
satya
  • 191
  • 2
  • 7
12

You could do it like this:

f.addWindowListener(new WindowAdapter(){
  public void windowClosing(WindowEvent we){
    System.exit(0);
  }
});
rubeh
  • 153
  • 3
  • 10
  • 1
    While this is literally the answer to his title, his question body has this code in it already. I believe his System.exit(0) is not acting as expected (possibly the same problem I'm having, and looking for the answer for). – Kit10 Jan 08 '14 at 21:47
3

Try doing it like this:

class ExampleClass implements ActionListener, WindowListener
{

...

f.addWindowListener(this);

...

public void windowDeactivated(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowOpened(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}

public void windowClosing(WindowEvent e) 
{
    System.exit(0);
}

}
jobukkit
  • 2,490
  • 8
  • 26
  • 41