当我使用具有Alpha值的前景色的JLabel时,如下所示:
JLabel label = new JLabel( "This is non HTML text with correct alpha color" );
label.setForeground( new Color( 1.0f, 1.0f, 1.0f, 0.5f) );标签以0.5-alpha正确显示,因此为50%灰色。
但当我将文本更改为HTML (以更好地控制文本呈现)时,如下所示:
JLabel label = new JLabel( "<html><p>This is HTML text with incorrect alpha color</p></html>" );
label.setForeground( new Color( 1.0f, 1.0f, 1.0f, 0.5f) );那么标签是100%白色的。看起来,在渲染HTML的时候,前景色的alpha值被忽略了。
我使用的是Windows7 64位下的Java 1.6.0_26 (32位)。
这是一个bug还是一个已知的限制,或者我在这里做错了什么?
发布于 2012-10-01 01:27:42
不能将HTML代码和setForeground样式混合在一起。
请参阅来自oracle的JLabel html text ignores setFont和如何使用JLabels(1)教程。
只需使用HTML或JLabel样式。
发布于 2012-10-01 05:14:33
为了给我自己的问题一个可能的答案,我刚刚找到了一种用HTML渲染来实现alpha透明度的方法。只需覆盖JLabel的"paintComponent“方法,并对给定的Graphics2D实例使用AlphaComposite:
@Override
protected void paintComponent( Graphics g )
{
Composite alphaComposite = AlphaComposite.getInstance( AlphaComposite.SRC_OVER, 0.5f );
Graphics2D g2d = (Graphics2D)g.create();
g2d.setComposite( alphaComposite );
super.paintComponent( g2d );
}https://stackoverflow.com/questions/12663268
复制相似问题