Why not to draw directly inside JFrame [duplicate]

南笙酒味 提交于 2019-11-26 17:22:46

问题


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 ?

Thanks in advance.


回答1:


Three main reasons...

  1. Top level containers aren't double buffered, which cause flickering when the frame is repainted, yes, you can implement you're own double buffering, but...
  2. Painting inside a frame does not take into consideration the frames borders, meaning that it's possible to paint under them. Frame borders are also platform/look and feel specific, meaning that there sizes change
  3. In the case of Swing windows, there are components that already exist on the window, meaning that they paint over (or be painted over), in most cases, both (because of the optimised painting engine in Swing), making it difficult to produce a reasonable result

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

Take a look at...

  • 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?

for more details

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



来源:https://stackoverflow.com/questions/24026743/why-not-to-draw-directly-inside-jframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!