如何关闭窗口AWT? [英] How to close the window in awt?

查看:152
本文介绍了如何关闭窗口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()

<一个href=\"http://stackoverflow.com/questions/1407161/why-should-you-have-to-dispose-a-java-awt-window-that-goes-out-of-scope\">Why你应该有处置()一java.awt.Window中的超出范围?

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

这篇关于如何关闭窗口AWT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆