为什么在顶级容器中覆盖油漆这么糟糕? [英] Why is overriding paint in a top-level container so bad?

查看:92
本文介绍了为什么在顶级容器中覆盖油漆这么糟糕?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么要覆盖 paint()方法是太可怕了/components/toplevel.htmlrel =nofollow>顶级容器,如 JFrame ?可能会导致什么样的问题?

Why is it so terrible to override the paint() method of a top-level container like JFrame? What kind of problems can it cause?

推荐答案

有很多原因,但是很多也将依赖于您所使用的工具包使用。

There are a number of reasons, but many will also depend on the toolkit you are using.

一般来说,在顶级容器上使用 paint 会将您锁定到给定的容器中(即框架 Applet )。这还会影响您重新使用组件的能力,因为Windows无法添加到另一个窗口。

Generally speaking, using paint on a top level container locks you into a given container (ie Frame or Applet). This also effects you ability to reuse the component, as windows can't be added to another window.

另一个问题是大多数框架都添加了边框(帧边框,通常保存标题和框架控件)。绘画到顶级容器不考虑这些插入,这意味着如果您在位置0x0绘制,您将在框架的标题栏下绘画。

The other issue is that most frames have borders added to them (the frames border which usually holds the title and frame controls). Painting to the top level container does not take into consideration these insets, meaning that if you paint at location 0x0, you will be painting under the title bar of the frame.

在Swing下,顶级容器不是双缓冲的。这意味着渲染趋向于闪烁,因为每个元素都直接渲染到图形图层,而不是单次绘制。

Under Swing, top level containers are not double buffered. This means that repaints tend to flicker as each element is rendered directly to graphics layer individual, rather then being painted in a single pass.

同样在Swing中,顶层容器由 JRootPane 控制,它将容器的顶部放置一个内容窗格(如果您正在使用它,菜单栏)。这意味着您的绘画工作可能被这些组件所覆盖。

Also in Swing, the top level containers are controlled by a JRootPane, which places a content pane (and if you're using it, menu bar) on-top of the container. This means that it is possible for your painting efforts to overridden by these components.

我还会添加(在挥杆下)组件油漆层/链条更灵活,允许您有机会在不同的图层进行绘制,虽然一般来说,您应该使用 paintComponent

I would also add (under swing) that the component paint layer/chain is more flexible, allowing you the opportunity to paint at different layers, although generally speaking, you should be using paintComponent.

这篇关于为什么在顶级容器中覆盖油漆这么糟糕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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