Java Nimbus LAF与透明文本字段 [英] Java Nimbus LAF with transparent text fields

查看:84
本文介绍了Java Nimbus LAF与透明文本字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序在几个透明的地方使用禁用的JTextFields - 允许显示背景而不是文本字段的正常背景。

I have an application that uses disabled JTextFields in several places which are intended to be transparent - allowing the background to show through instead of the text field's normal background.

运行新的Nimbus LAF时,这些字段是不透明的(尽管设置了setOpaque(false)),并且我的UI被破坏了。好像LAF忽略了不透明的属性。明确地设置背景颜色在几个地方都很难,并且由于背景图像而不是最佳实际上不起作用 - 它仍然在顶部描绘LAF默认背景,留下边框状外观(下面的启动画面背景显式设置为与图像匹配)。

When running the new Nimbus LAF these fields are opaque (despite setting setOpaque(false)), and my UI is broken. It's as if the LAF is ignoring the opaque property. Setting a background color explicitly is both difficult in several places, and less than optimal due to background images actually doesn't work - it still paints it's LAF default background over the top, leaving a border-like appearance (the splash screen below has the background explicitly set to match the image).

关于如何让Nimbus不为JTextField绘制背景的任何想法?

Any ideas on how I can get Nimbus to not paint the background for a JTextField?

注意:我需要一个JTextField而不是JLabel,因为我需要线程安全的setText()和包装功能。

Note: I need a JTextField, rather than a JLabel, because I need the thread-safe setText(), and wrapping capability.

注意:我的后备位置是继续使用系统LAF,但是Nimbus确实看起来好多了。

Note: My fallback position is to continue using the system LAF, but Nimbus does look substantially better.

参见下面的示例图片。

此行为的意外是由于误解了setOpaque()的意图 - 来自Nimbus错误报告:

The surprise at this behavior is due to a misinterpretation of what setOpaque() is meant to do - from the Nimbus bug report:


这是一个问题他对Swing的原始设计以及它多年来一直令人困惑。问题是setOpaque(false)在退出LAF时产生了副作用,即隐藏背景并不是真正的背景。可以说我的组件有透明部分和swing应该在其后面绘制父组件。

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

不幸的是,Nimbus组件也似乎不尊重setBackground(null),否则这将是停止背景绘制的推荐方法。设置一个完全透明的背景对我来说似乎不直观。

It's unfortunate that the Nimbus components also appear not to honor setBackground(null) which would otherwise be the recommended way to stop the background painting. Setting a fully transparent background seems unintuitive to me.

在我看来,setOpaque()/ isOpaque()是一个错误的公共API选择,应该只是:

In my opinion, setOpaque()/isOpaque() is a faulty public API choice which should have been only:

public boolean isFullyOpaque();

我这样说,因为isOpaque()== true是与Swing的合约,组件子类将负责绘制它的整个背景 - 这意味着父母可以跳过绘制该区域(如果它需要)(这是一个重要的性能增强)。外部的东西不能直接改变这个合同(合法地),其履行可以被编码到组件中。

I say this, because isOpaque()==true is a contract with Swing that the component subclass will take responsibility for painting it's entire background - which means the parent can skip painting that region if it wants (which is an important performance enhancement). Something external cannot directly change this contract (legitimately), whose fulfillment may be coded into the component.

因此,不应使用setOpaque()设置组件的不透明度。相反,像setBackground(null)之类的东西会导致许多组件没有背景,因此变得不完全不透明。举例来说,在理想的世界中,大多数组件应该具有如下所示的isOpaque():

So the opacity of the component should not have been settable using setOpaque(). Instead something like setBackground(null) should cause many components to "not have a background" and therefore become not fully opaque. By way of example, in an ideal world most components should have an isOpaque() that looks like this:

public boolean isOpaque() { return (background!=null); }






示例http://i41.tinypic.com/sviczq.png

替代文字http://i44.tinypic.com/35d80ao.png

推荐答案

上周我使用JTextPane遇到了同样的问题。当使用除nimbus之外的任何外观时,setOpaque()方法按预期工作。显然,nimbus的外观和感觉改变了我们对许多组件的setOpaque()所期望的行为。根据您的看法,它可以被视为一个错误。查看关于这个sun bugid的评论:

I ran into this same issue last week using JTextPane. The setOpaque() method works as expected when using any look and feel other than nimbus. Apparently, the nimbus look and feel changes the behaviour we have come to expect with setOpaque() for many Components. Depending on how you look at it, it can be considered a bug. Check the comments on this sun bugid:

nimbus opaque bug

对我有用的解决方法是:

The workaround that worked for me was:

myPane.setOpaque(false); // added by OP
myPane.setBorder(BorderFactory.createEmptyBorder());
myPane.setBackground(new Color(0,0,0,0));






OP注意:我还必须确保setOpaque (假)对于JTextField,以便绘制父背景 - 只是想为其他人提及这一点,以防他们尝试使用setOpaque(true),就像我一样。


Note from OP: I also had to ensure setOpaque(false) for JTextField so that the parent background was painted - just wanted to mention this for others who follow in case they had experimented with setOpaque(true), as I had.

这篇关于Java Nimbus LAF与透明文本字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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