从 JButton 进程中关闭 JFrame 保持活动状态

发布于 2024-12-10 03:41:04 字数 1365 浏览 0 评论 0原文

我有一个用 windowbuilderpro 开发的类,我想从 JButton 关闭它,而不是使用窗口上的标准 X 按钮,所以这里是该类的示例:

public class MainWindow {

public JFrame frame;

public MainWindow() {
    initialize();
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  }

public void show() {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                //Show the main Frame
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }   
        }
    });
}

当我从 X 按钮关闭窗口时,窗口正确关闭,并且进程终止。

当我从具有此侦听器的 JButton 关闭时:

mntmExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
               //Close the application main form
               frame.setVisible(false);
               frame.dispose();
            }
        });

框架窗口正确关闭,但进程保持活动状态......为什么?

Process Alive

如您所见,有一个连续启动和终止的 AWT-Shutdown 线程,我怎样才能实现相同的行为关闭应用程序进程的 X 按钮?

注释:

System.exit(0);不合适,因为如果有另一个后台运行线程并且我不希望这样,它也会终止应用程序。 MainWindow 类应该关闭并释放它的资源,与使用关闭 MainWindow 实例的 X 按钮关闭应用程序的行为相同,但如果有后台线程正在运行,它不会杀死它们,而是等到它们完成工作...

环境:

  • JDK 7
  • Eclipse 3.7.1

I have a class developed with windowbuilderpro that i want to close also from a JButton further than with the standard X button on the window, so here the example of the class :

public class MainWindow {

public JFrame frame;

public MainWindow() {
    initialize();
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  }

public void show() {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                //Show the main Frame
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }   
        }
    });
}

When i close the window from the X button the window close correctly and the process terminate.

When i close instead from a JButton that have this listener :

mntmExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
               //Close the application main form
               frame.setVisible(false);
               frame.dispose();
            }
        });

the frame window close correctly but the process remain alive ... Why ?

Process Alive

As you can see there is an AWT-Shutdown thread that start and terminate continuously, How can i achieve the same behaviour of the X button that close also the application process ?

Notes :

System.exit(0); is not suitable because it terminate the application also if there are another background running thread and i don't want that . The MainWindow class should close and release it's resource, the same behaviour that have closing the application with the X button that close the MainWindow instance but if there are background thread running it doesn't kill they but wait until they finished their work...

Enviroment :

  • JDK 7
  • Eclipse 3.7.1

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

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

发布评论

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

评论(3

怀中猫帐中妖 2024-12-17 03:41:04

不确定您真正需要什么,看起来您再次创建新的 JFrame,不要这样做,创建 JFrame 一次并重新使用此容器,

frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); // do nothing

frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); // same as setVisible(false)

然后为了可见,您只能调用 frame.setVisible(true );

更舒适的是覆盖 WindowListener,那么就可以控制一些Events

not sure what you really needed, that looks like that you create new JFrame again an again, don't do that, create JFrame once and re-use this Container

frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); // do nothing

frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); // same as setVisible(false)

then for visibily you can only to call frame.setVisible(true);

for more Confortable is override WindowListener, then you can control some Events

月光色 2024-12-17 03:41:04

当激活 x 按钮或 Exit 按钮时,此代码中的所有线程都会停止。你有不同的行为吗?

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

public class MainWindow {

    public JFrame frame;
    JButton mntmExit = new JButton("Exit");

    public MainWindow() {
        frame = new JFrame("Close Me!");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        mntmExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //Close the application main form
                frame.setVisible(false);
                frame.dispose();
            }
        });
        frame.add(mntmExit);
        frame.pack();
        show();
    }

    public void show() {
        //Show the main Frame
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                MainWindow mw = new MainWindow();
                mw.show();
            }
        });
    }
}

All threads in this code stop when either the x button or the Exit button are activated. Are you getting different behavior?

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

public class MainWindow {

    public JFrame frame;
    JButton mntmExit = new JButton("Exit");

    public MainWindow() {
        frame = new JFrame("Close Me!");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        mntmExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //Close the application main form
                frame.setVisible(false);
                frame.dispose();
            }
        });
        frame.add(mntmExit);
        frame.pack();
        show();
    }

    public void show() {
        //Show the main Frame
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                MainWindow mw = new MainWindow();
                mw.show();
            }
        });
    }
}
始终不够爱げ你 2024-12-17 03:41:04

只需添加一行:

System.exit(0);

Just add one line:

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