使用代码关闭java框架

发布于 2024-11-04 05:26:51 字数 923 浏览 1 评论 0原文

可能的重复:
如何以编程方式关闭 JFrame

我正在使用 JFrame 开发 java GUI。我想关闭 GUI 框架并通过代码将其处理掉。 我已经实现:

topFrame.addWindowListener(new WindowListener()
        {
            public void windowClosing(WindowEvent e)
            {
                emsClient.close();
            }
            public void windowOpened(WindowEvent e) {
            }
            public void windowClosed(WindowEvent e) {
            }
            public void windowIconified(WindowEvent e) {
            }
            public void windowDeiconified(WindowEvent e) {
            }
            public void windowActivated(WindowEvent e) {
            }
            public void windowDeactivated(WindowEvent e) {
            }
        });`

如何调用 windowClosing 事件?或者还有其他办法吗?

Possible Duplicate:
How to programmatically close a JFrame

I am developing a java GUI using JFrame. I want to close the GUI frame and dispose it off through code.
I have implemented :

topFrame.addWindowListener(new WindowListener()
        {
            public void windowClosing(WindowEvent e)
            {
                emsClient.close();
            }
            public void windowOpened(WindowEvent e) {
            }
            public void windowClosed(WindowEvent e) {
            }
            public void windowIconified(WindowEvent e) {
            }
            public void windowDeiconified(WindowEvent e) {
            }
            public void windowActivated(WindowEvent e) {
            }
            public void windowDeactivated(WindowEvent e) {
            }
        });`

How can I invoke the windowClosing event?? Or is there some other way?

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

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

发布评论

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

评论(4

遗心遗梦遗幸福 2024-11-11 05:26:51

这将以编程方式触发窗口关闭事件:

topFrame.dispatchEvent(new WindowEvent(topFrame, WindowEvent.WINDOW_CLOSING));

如果你想关闭框架,你需要调用:

topFrame.dispose();

This will programmatically trigger the window closing event:

topFrame.dispatchEvent(new WindowEvent(topFrame, WindowEvent.WINDOW_CLOSING));

If you want to close the frame you need to call:

topFrame.dispose();
孤凫 2024-11-11 05:26:51

调用 dispose() 方法怎么样?

How about invoking dispose() method?

心不设防 2024-11-11 05:26:51

你需要这个:

yourFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

你可以在构造函数中添加该行(不要忘记它)。

You need this:

yourFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

You can add that line in the constructor(Dont forget it).

╰つ倒转 2024-11-11 05:26:51
import java.awt.event.*;
import javax.swing.*;

class CloseFrame {

    public static void main(String[] args) {

        Runnable r = new Runnable() {

            public void run() {
                JButton close = new JButton("Close me programmatically");
                final JFrame f = new JFrame("Close Me");
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.setContentPane( close );
                close.addActionListener( new ActionListener(){
                    public void actionPerformed(ActionEvent ae) {
                        // make the app. end (programatically)
                        f.dispose();
                    }
                } );
                f.pack();
                f.setLocationByPlatform(true);
                f.setVisible(true);
            }
        };

        SwingUtilities.invokeLater(r);
    }
}
import java.awt.event.*;
import javax.swing.*;

class CloseFrame {

    public static void main(String[] args) {

        Runnable r = new Runnable() {

            public void run() {
                JButton close = new JButton("Close me programmatically");
                final JFrame f = new JFrame("Close Me");
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.setContentPane( close );
                close.addActionListener( new ActionListener(){
                    public void actionPerformed(ActionEvent ae) {
                        // make the app. end (programatically)
                        f.dispose();
                    }
                } );
                f.pack();
                f.setLocationByPlatform(true);
                f.setVisible(true);
            }
        };

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