为什么我的Swing窗口会在几秒钟后自动关闭? [英] Why does my Swing window keep closing itself after a few seconds?

查看:230
本文介绍了为什么我的Swing窗口会在几秒钟后自动关闭?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:如果多个标签令人困惑,我在Jython工作。

In case the multiple tags are confusing, I'm working in Jython.

这是我的 SSCCE

from javax.swing import JFrame

window = JFrame('Test', 
                defaultCloseOperation = JFrame.EXIT_ON_CLOSE,
                size = (800, 600))
window.visible = True

窗口打开,在那里停留几秒钟,然后关闭。到目前为止,我发现的唯一解决方案是添加而True:传递到最后,这似乎表明问题是窗口超出范围,因此它会被清理并且必须关闭。事实上这可能是我遇到同样问题的另一个症状以前遇到过的事情

The window opens, sits there for a few seconds, and then closes. So far the only solution I've found is to add while True: pass to the end, which would seem to indicate that the problem is that window is going out of scope, so it gets cleaned up and has to close. This may in fact be another symptom of the same problem I have encountered previously.

但是,我不认为在无限循环中浪费周期是最佳方案。我想我可以通过在每个循环上睡几秒来减少问题,但我仍然希望正确地解决这个

However, I don't think wasting cycles in an infinite loop is the best solution. I guess I could make it less of an issue by sleeping for a few seconds on each loop, but I would still like to solve this properly.

以正确的方式做事,在EDT上创建窗口,给出完全相同的行为:

Doing things the 'right way', creating the window on the EDT, gives exactly the same behaviour:

from javax.swing import JFrame, SwingUtilities
from java.lang import Runnable

class Foo(Runnable):
    def run(self):
        window = JFrame('Test', 
                        defaultCloseOperation = JFrame.EXIT_ON_CLOSE,
                        size = (800, 600))
        window.visible = True

foo = Foo()
SwingUtilities.invokeLater(foo)

在以前的应用程序中,这不是问题,因为我需要无论如何,其他任务的无限循环(监视套接字等)。但是,我当前的应用程序完全由用户输入驱动,所以在 invokeLater()之后我不需要/想要任何东西。

In previous applications this hasn't been a problem, because I've needed an infinite loop for other tasks anyway (monitoring sockets etc). However, my current application is driven purely by user input, so I don't need/want anything after I invokeLater().

更新:根据kingo的回答,我尝试将其中一个实例化:

Update: Based on kingo's answer, I tried instantiating one of these:

class JFrameTest(JFrame):
    def addNotify(self):
        print 'In addNotify()'
        JFrame.addNotify(self)

    def removeNotify(self):
        print "In removeNotify()"
        JFrame.removeNotify(self)

在addNotify()中打印,但不是在removeNotify()中,窗口的行为相同。为什么 removeNotify()没有被调用?

"In addNotify()" is printed, but not "In removeNotify()", and the window behaves the same. Why would removeNotify() not be being called?

另外,我试过做 window.setVisible(True)而不是 window.visible = True ,这也没有效果。

Also, I have tried doing window.setVisible(True) rather than window.visible = True, and that has no effect either.

推荐答案

我建议采用以下策略来了解有关此问题的更多信息:

I suggest the following strategy to learn more about the problem:

sun .awt.AWTAutoShutdown 是防止在使用工具包注册本机窗口对等项时关闭JVM的类。当 addNotify()被调用时,任何组件都会被注册。对于一个帧,这是在你调用 setVisible(true)时完成的。

sun.awt.AWTAutoShutdown is the class that prevents shutdown of the JVM if a native window peer is registered with the toolkit. Any component is registered when addNotify() is called on it. For a frame, this is done when you call setVisible(true).

同伴可以获得的唯一方式unregistered是指有人在对等方上调用 dispose()。在JRE中对等体上唯一调用 dispose()的地方来自 Component #removeNotify()

The only way a peer can get unregistered is if somebody calls dispose() on the peer. The only place dispose() is called on a peer in the JRE is from Component#removeNotify().

您可以在帧类中覆盖该方法并打印堆栈跟踪以查看发生这种情况的原因。

You could override that method in your frame class and print a stack trace to see why that happens.

这篇关于为什么我的Swing窗口会在几秒钟后自动关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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