setOpaque(真/假); Java的 [英] setOpaque(true/false); Java

查看:143
本文介绍了setOpaque(真/假); Java的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java2D中,当你使用setOpaque的时候,我对true和false有些困惑。例如,我知道Swing Opaque意思是当绘制Swing将不会绘制组件背后的内容。或者这是倒退?哪一个是它的?



谢谢

解决方案

你的问题是,不透明在英文中被定义为完全不透明。因此,一个不透明的组件是一个绘制整个矩形的组件,并且每个像素在任何程度上都不是半透明的。

然而,Swing组件opacity API就是其中之一错误设计,因此经常被误用的API。



重要的是要明白 isOpaque 是一个 Swing系统和特定组件之间的契约。如果它返回true,那么该组件保证以非半透明方式绘制其矩形区域的每个像素。此API 应该是抽象的,以强制所有组件编写者考虑它。 Swing的绘画系统使用 isOpaque API来确定给定组件覆盖的区域是否必须绘制 ,包括组件的容器和祖先。如果一个组件返回true,这个Swing系统可以优化绘画,直到调用特定组件的绘制方法时才绘制该区域中的任何东西。



由于合约含义 isOpaque ,API setOpaque 不应该存在,因为它对于任何外部调用 setOpaque ,因为外部事物不能知道有问题的组件是否(甚至可以)承认它。相反,isOpaque应该已被每个具体组件覆盖,以返回它实际上是否是不透明的,因为它是当前属性。



由于 setOpaque API does 存在,许多组件错误地实现了它(可以理解)来驱动它们是否绘制它们的背景(例如JLabel和JPanel填充他们的背景颜色)。这样做的效果是,通过API的用户创建一个印象,认为 setOpaque 驱动该背景是否应该绘制,但它不会T。



此外,如果您想要为半透明背景绘制JLabel,则需要使用Alpha值设置背景颜色,然后执行setOpaque(true),但它实际上并不透明 - 它是半透明;它背后的组件仍然需要绘制,以便组件正确呈现。



Java 6的新Nimbus Look&感觉。有关于针对Nimbus提交的透明组件的众多错误报告(请参阅堆栈溢出问题 Java Nimbus LAF与透明文本字段)。 Nimbus开发团队的回应是这样的:
$ b


这是Swing原始设计中的一个问题,以及它如何混淆多年。问题是setOpaque(false)在[现有的] LAF中有一个副作用,那就是隐藏背景,而不是真正意义上的背景。这意味着该组件可能有透明部分,[Swing]应该在其后面绘制父组件。


所以总之,你不应该使用 setOpaque 。如果你使用它,请记住,一些Look&感觉和一些组件可能会做令人惊讶的事情。最后,实际上没有正确的答案。

In Java2D when you use setOpaque I am a little confused on what the true and false does.

For example I know that in Swing Opaque means that when painting Swing wont paint what is behind the component. Or is this backwards? Which one is it?

Thanks

解决方案

The short answer to your question is that "opaque" is defined in English as completely non-transparent. Therefore an opaque component is one which paints it's entire rectangle, and every pixel is not at all translucent to any degree.

However, the Swing component opacity API is one of those mis-designed and therefore often mis-used APIs.

What's important to understand is that isOpaque is a contract between the Swing system and a particular component. If it returns true, the component guarantees to non-translucently paint every pixel of it's rectangular area. This API should have been abstract to force all component writers to consider it. The isOpaque API is used by Swing's painting system to determine whether the area covered by a given component must be painted for components which overlap it and which are behind it, including the component's container and ancestors. If a component returns true to this API, the Swing system may optimize painting to not paint anything in that area until invoking the specific component's paint method.

Because of contractual implication of isOpaque, the API setOpaque should not exist, since it is actually incorrect for anything external to call setOpaque since, in turn, the external thing can't know whether the component in question will (or even can) honor it. Instead, isOpaque should have been overridden by each concrete component to return whether it actually is, in fact, opaque given it's current properties.

Because the setOpaque API does exist, many components have mis-implemented it (quite understandably) to drive whether or not they will paint their "background" (for example JLabel and JPanel filling with their background color). The effect of this is to create an impression with users of the API to think that setOpaque drives whether or not that background should paint, but it doesn't.

Furthermore, if, say, you wish to paint a JLabel with a translucent background you need to set a background color with an alpha value, and do setOpaque(true), but it's not actually opaque - it's translucent; the components behind it still need to paint in order for the component to render properly.

This problem was exposed in a significant way with the Java 6's new Nimbus Look & Feel. There are numerous bug reports regarding transparent components filed against Nimbus (see stack overflow question Java Nimbus LAF with transparent text fields). The response of the Nimbus development team is this :

This is a problem [in] the orginal design of Swing and how it has been confusing for years. The issue is setOpaque(false) has had a side effect in [existing] LAFs which is that of hiding the background which is not really what it is [meant] for. It is [meant] to say that the component may have transparent parts and [Swing] should paint the parent component behind it.

So, in summary, you should not use setOpaque. If you do use it, bear in mind that the combination of some Look & Feels and some components may do "surprising" things. And, in the end, there is actually no right answer.

这篇关于setOpaque(真/假); Java的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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