如何关闭窗口AWT?

如何关闭窗口AWT?

问题描述:

我在创建一个AWT劳工处。aplication,当我关闭窗口,关闭按钮不工作,但我已经添加了关闭按钮的功能,在这之后关闭按钮不起作用...

I am creating a samll aplication in awt, when i am closing the window, closing button is not working, but i have added the functionality of closing button, after that closing button is not working...

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");
}


}

为什么关闭按钮不起作用?

why closing button is not working?

这是更好地使用的方法公共无效的Dispose()

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

Why你应该有处置()一java.awt.Window中的超出范围?

f.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent we) {
            dispose();
         }
     }
);