JDialog 无法打包(),仅有时
我用 Java 编写了一个漂亮的东西,其中包含一个 JDialog,它一开始相当小,然后使用 pack() 来容纳程序稍后放入其中的内容。所有这一切都在 JDialog 渲染之前进行。
然后,令我惊讶的是,大约 80% 的情况下,当我运行它时,窗口无法调整大小。它似乎完全是随机的,因为理论上它每次都做完全相同的事情。到底为什么五秒后它会在同一台机器上使用相同的代码执行不同的操作?
顺便说一句,当我为此 GUI 启用本机 Windows 外观时,出现了这个问题。
I have written a nifty thing in Java with a GUI that includes a JDialog that starts out rather small and then uses pack() to accommodate things the progam later puts in it. All of this is going on before the JDialog renders.
Then, to my surprise, about 80% of the time, when I run it, the window fails to resize. It seems to be entirely random, as it's theoretically doing exactly the same thing every time. Why on Earth would it do something different with the same code on the same machine five seconds later?
This problem, by the way, popped up when I enabled the native Windows look-and-feel for this GUI.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据我的经验,当 GUI 执行类似这样的随机有趣的操作时,可能是没有在事件调度线程上执行所有 GUI 调用的症状。
确保来自非 GUI 线程的所有 GUI 调用都包装在 SwingUtilities.invokeLater 或 invokeAndWait 中。
快速谷歌搜索发现了一种检查应用程序是否符合 EDT 规则的好方法:http://thejavacodemonkey.blogspot.com/2007/08/using-aspectj-to-detect-violations-of.html
In my experience, when the GUI does random funny stuff like this, it might be a symptom of not doing all your GUI calls on the Event Dispatch Thread.
Make sure all your GUI calls from non-GUI threads are wrapped in SwingUtilities.invokeLater or invokeAndWait.
A quick google search turned up what seems to be a nifty way to check that your application conforms to the EDT-rules: http://thejavacodemonkey.blogspot.com/2007/08/using-aspectj-to-detect-violations-of.html