Java窗口绘制问题
我的应用程序在启动时不会自行绘制。当调整它的大小或最小化/最大化它时,应用程序的窗口被绘制。
此问题仅出现在 Windows 机器上(我测试了 XP、Vista 和 Windows 7)。在 Mac OS X 和 Linux 上,该应用程序运行良好。
机器已经安装了java 6。我的应用程序使用AWT,所以不是Swing。我尝试使用 Swing(因此使用 JFrame 而不是 Frame),但这并不能解决问题。
我检查了框架的 repaint()、update() 和 Paint() 的调用。它们全部出现,并且可以绘制图像。我还检查了这些调用是否在 EDT 线程上完成。情况就是这样。当调整窗口大小(或最小/最大调整)时,系统将调用paint(),并绘制图像。
我担心我错过了一些非常明显的东西。我使框架可见,验证它(也使用无效进行测试)并重新绘制它。这在 Mac OS X 和 Linux 中就足够了。
有人对我应该做什么或还可以尝试什么有任何建议吗?
预先感谢
莫里斯
My application does not paint itself on startup. When resizing it or minimizing/maximizing it, the window of the application is painted.
This problem only appears on Windows machines (I tested XP, Vista and Windows 7). On Mac OS X and Linux, the application works fine.
The machines have installed java 6. My application uses the AWT, so not Swing. I tried using Swing (so JFrame in stead of Frame), but this does not solve the problem.
I checked the calls on repaint(), update() and paint() of the frame. They all appear, and the image to draw is available. I also checked if these calls are done on the EDT thread. This is the case. When the window is resized (or min/max-ed) a call on paint() is done by the system, and the image is drawn.
My fear is that I'm missing something really obvious. I'm making the frame visible, validate it (also tested with invalidate) and repaint it. This is sufficient in Mac OS X and Linux.
Does somebody have any suggestions to what I should do, or what else to try?
Thanx in advance
Maurice
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我的猜测是你正在重写 Frame.paint 。通过绘制到 JPanel(或 Canvas,如果您坚持使用过时的 AWT),您可能会获得更好的结果,并且当您这样做时,请确保您覆盖 JPanel 的paintComponent,而不是绘制。在某些情况下,带有 ImageIcon 的 JLabel 可能更简单。
不过,正如其他人所建议的,尝试发布一个测试用例。
My guess is that you are overriding Frame.paint. You will probably get better results from painting to a JPanel (or Canvas, if you insist on the obsolete AWT), and when you do that, make sure you're overriding JPanel's paintComponent, rather than paint. In some cases, a JLabel with an ImageIcon can be simpler still.
As others have suggested, though, try to post a test case.
您可能想使用自定义 RepaintManager 来检查此调试技巧。它帮助我发现了一些棘手的间歇性挥杆故障。
http://weblogs.java.net/blog/alexfromsun/archive /2006/02/debugging_swing.html
You might want to check out this debugging trick using a custom RepaintManager. It helped me track down some tricky intermittent swing glitches.
http://weblogs.java.net/blog/alexfromsun/archive/2006/02/debugging_swing.html
不看任何源代码很难诊断问题,但是图像是否已完全加载?
AWT 之后在后台加载图像,因此即使调用
Toolkit.getImage()
可能会返回有效的Image
实例,但这并不意味着图像是由那个时候。您可以使用 MediaTracker 来跟踪这个。It is difficult to diagnose the problem without seeing any source code, but is the image fully loaded?
AWT loads images in the background after, so even though a call to
Toolkit.getImage()
may return a validImage
instance, this does not mean that the image is loaded by that time. You can use a MediaTracker to track this.