为什么不直接在JFrame中绘制 [英] Why not to draw directly inside JFrame

查看:117
本文介绍了为什么不直接在JFrame中绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以解释为什么我不应该使用 paint 方法直接在 JFrame 窗口内绘制,并且我应该在 JFrame 中使用 paintComponent 方法和 JPanel

Can anyone explain me why shouldn't i use paint method to draw directly inside a JFrame window, and i should use paintComponent method with a JPanel inside the JFrame ?

提前致谢。

推荐答案

三个主要原因.. 。


  1. 顶级容器不是双缓冲的,当重新绘制框架时会导致闪烁,是的,你可以实现你自己的双缓冲,但是......

  2. 在框架内绘画不考虑框架边框,这意味着可以在它们下面绘画。框架边框也是平台/外观特定的,这意味着尺寸发生变化

  3. 在Swing窗口的情况下,窗口上已经存在组件,这意味着它们会覆盖(或者被涂上),在大多数情况下,两者(因为Swing中优化的绘画引擎),使得难以产生合理的结果

对于第2点,框架装饰在窗口范围内绘制,而不是在它们之外

For point #2, frame decorations are painted WITHIN the "window" bounds, not outside them

看看......

  • How to get the EXACT middle of a screen, even when re-sized
  • Java AWT drawString() does not display on window
  • How can I set in the midst?

了解更多详情

请花时间阅读在AWT和Swing中绘画执行自定义绘画以获取更多详细信息

Make the time to read through Painting in AWT and Swing and Performing Custom Painting for more details

这篇关于为什么不直接在JFrame中绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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