AWT中如何关闭窗口?

发布于 2024-10-21 17:45:08 字数 980 浏览 2 评论 0原文

我正在使用 AWT 创建一个小型应用程序。当我尝试关闭窗口时,“关闭”按钮不起作用。

这是我的代码:

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

如何修复“关闭”按钮?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不再见 2024-10-28 17:45:08

最好使用方法 public void dispose()

为什么必须 dispose() 超出范围的 java.awt.Window?

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

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();
         }
     }
);
小…楫夜泊 2024-10-28 17:45:08

你可以这样做:

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

You could do it like this:

f.addWindowListener(new WindowAdapter(){
  public void windowClosing(WindowEvent we){
    System.exit(0);
  }
});
我的奇迹 2024-10-28 17:45:08

尝试这样做:

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

}

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

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文