简体   繁体   English

如何避免JFrame EXIT_ON_CLOSE操作退出整个应用程序?

[英]How to avoid JFrame EXIT_ON_CLOSE operation to exit the entire application?

I have a application that launches other applications, something like a dock. 我有一个应用程序启动其他应用程序,如Dock。 The problem is that if the app that I'm launching ( JFrame ) has the EXIT_ON_CLOSE it will also close my main application. 问题是,如果我正在启动的应用程序( JFrame )具有EXIT_ON_CLOSE它也将关闭我的主应用程序。

I have no control what-so-ever over the applications that I'm launching. 我无法控制我正在推出的应用程序。 That is, I cannot expect the application to have a good behavior and use DISPOSE_ON_CLOSE . 也就是说,我不能指望应用程序具有良好的行为并使用DISPOSE_ON_CLOSE

What can I do to avoid this? 我该怎么做才能避免这种情况? I've tried already to use threads, but no luck. 我已经尝试过使用线程,但没有运气。 I also tried to put the main application thread in daemon, but no luck too. 我也尝试将主应用程序线程放在守护进程中,但也没有运气。

I've tried putting a custom SecurityManager overwritting the checkExit method. 我已经尝试将自定义SecurityManager覆盖checkExit方法。 The problem is that now even the main app can't exit. 问题是现在即使主应用程序也无法退出。 Also, it doesn`t "work" because applications that use EXIT_ON_CLOSE as their default close operation will throw a Exception and not execute (since Swing checks the Security Manager for the exit -- System.checkExit()), failing to launch :(. 此外,它不起作用,因为使用EXIT_ON_CLOSE作为其默认关闭操作的应用程序将抛出异​​常而不执行(因为Swing检查安全管理器的退出 - System.checkExit()),无法启动:( 。

if you want to close only the frame you see, it should be DISPOSE_ON_CLOSE 如果你只想关闭你看到的帧,它应该是DISPOSE_ON_CLOSE

EDIT: 编辑:

Try intercepting the EXIT_ON_CLOSE event. 尝试拦截EXIT_ON_CLOSE事件。

frame.getGlassPane() or frame.getRootPane()

may be useful. 可能有用。

Also if you have right to execute methods of the other app, you can call setDefaultCloseOperation again setting it to DISPOSE_ON_CLOSE 此外,如果您有权执行其他应用程序的方法,则可以再次调用setDefaultCloseOperation将其设置为DISPOSE_ON_CLOSE

It is a bit of a hack, but you can always use a SecurityManager to manage the other frames. 这有点像黑客攻击,但您始终可以使用SecurityManager来管理其他帧。

This simple example prevents a frame from exiting: 这个简单的例子可以防止帧退出:

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

public class PreventExitExample {

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                PreventExitExample o = new PreventExitExample();

                JFrame f = new JFrame(o.getClass().getSimpleName());
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.setLocationByPlatform(true);
                f.setSize(new Dimension(400,200));
                f.setVisible(true);

                System.setSecurityManager(new PreventExitSecurityManager());
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

class PreventExitSecurityManager extends SecurityManager {

    @Override
    public void checkExit(int status) {
        throw new SecurityException("Cannot exit this frame!");
    }
}

You can get list of all frames using Frame's static method public static Frame[] getFrames() 你可以使用Frame的静态方法获得所有帧的列表public static Frame [] getFrames()

Iterate the list and check if a list member is instanceof JFrame change default close operation to DISPOSE_ON_CLOSE 迭代列表并检查列表成员是否是JFrame的实例,将默认关闭操作更改为DISPOSE_ON_CLOSE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM