Java 7,使用HTML格式标签时按钮文本的颜色

我有一些自定义UI用于某些按钮,通过inheritanceMetalButtonUI实现。 按钮使用HTML格式的标签。 这是我的要求,我需要支持多行按钮标签。

出于某种原因,当我的应用程序在Java 7上运行时(科学地更新4,最新),禁用按钮时的文本颜色现在是灰色的。 在Java 4或6上运行时不会发生这种情况。

在按钮标签的HTML中,我可以使用设置但是,当禁用该按钮时,将忽略此值。 好像在某个地方,禁用按钮时会覆盖我的字体颜色。 使用也无效。

我试过在UIDefaults中设置Button.disabledText。 这不是我真正想要做的,因为它会影响太多的按钮。 但无论如何,它对HTML格式的按钮标签无效。

MetalButtonUI定义了getDisabledTextColor,但实现它是无效的。

同样,实现paintText方法也无效。 不会为HTML格式的标签调用它。

我可以覆盖整个绘制方法,但这似乎是一个过于复杂的解决方案。

在这个领域有一个bug修复,报告已在Java 7中修复,请参阅http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4783068然而,错误报告对我来说并不是很清楚。 目前尚不清楚具体改变了什么,或者如何覆盖新行为。

有谁知道如何控制禁用按钮的文本颜色?

编辑:对不起,我应该从一开始就包含示例代码。 我的原始代码具有按钮和UI的自定义类,在UI类中使用自定义paint()方法。 但我现在看到核心问题可以非常简单地演示,只需调用button.setForeground(Color.black); 在Java 6中,这会影响启用和禁用状态的文本颜色。 在Java 7中,它仅影响启用状态。 mKorbel …感谢您帮助隔离问题!!!!

  import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class DisabledButtonDemo { public DisabledButtonDemo() { final JToggleButton button = new JToggleButton( "Button
Label"); // Next line sets the text color. // In Java 6 it is respected, for both enabled and disabled state. // In Java 7, it is only used for the enabled state. button.setForeground(Color.black); button.setPreferredSize(new Dimension(100, 100)); final JButton controlButton = new JButton( "Toggle Enabled/Disabled"); controlButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { button.setEnabled(!button.isEnabled()); } }); JFrame f = new JFrame("ButtonTest"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setLayout(new GridLayout(2,1)); f.add(button); f.add(controlButton); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { DisabledButtonDemo t = new DisabledButtonDemo(); } }); } }

编辑#2:Oracle现在将其作为一个错误进行跟踪,请参阅http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7176683

有谁知道如何控制禁用按钮的文本颜色?

其中一种方式(你的意思是Html)是

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

 import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.SwingUtilities; public class HtmlAndJButton { public HtmlAndJButton() { final String buttonText = " Whatever, but nothing wise"; final JButton button = new JButton(buttonText); JButton btn1 = new JButton("Toggle"); btn1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { button.setText("" + buttonText + ""); button.setEnabled(!button.isEnabled()); } }); JFrame f = new JFrame("ButtonTest"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setLayout(new GridLayout(2,1)); f.add(button); f.add(btn1); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { HtmlAndJButton t = new HtmlAndJButton(); } }); } } 

我阅读了您在问题上发布的错误报告,并提供了解决此问题的解决方法。 创建以下类:

 /** * Attaches to a JButton to work around Sun bug 4783068. * 

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4783068

*/ public final class SunBug4783068Fixer implements PropertyChangeListener { private static final SunBug4783068Fixer INSTANCE = new SunBug4783068Fixer(); public static void attach(AbstractButton to) { // Prevents adding it more than once to any single component. to.removePropertyChangeListener(INSTANCE); to.addPropertyChangeListener(INSTANCE); } public static void remove(AbstractButton from) { from.removePropertyChangeListener(INSTANCE); } public void propertyChange(PropertyChangeEvent evt) { if ((evt.getSource() instanceof AbstractButton) && "enabled".equals(evt.getPropertyName())) { AbstractButton target = (AbstractButton) evt.getSource(); target.setForeground(target.isEnabled() ? (Color) UIManager.getDefaults().get("Button.foreground") : (Color) UIManager.getDefaults().get("Button.disabledText")); } } }

然后将其添加到JToggleButton中

  final JButton controlButton = new JButton("Toggle Enabled/Disabled"); controlButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { button.setEnabled(!button.isEnabled()); } }); controlButton.addPropertyChangeListener(new SunBug4783068Fixer());