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

标签 java swing jbutton java-7

我有一个针对某些按钮的自定义 UI,通过子类化 MetalButtonUI 来实现。这些按钮使用 HTML 格式的标签。这对我来说是一个要求,我需要支持多行按钮标签。

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

在按钮标签的 HTML 中,我可以使用 <font color=..> 设置字体颜色但是,当按钮被禁用时,该值将被忽略。似乎在某个地方,当按钮被禁用时,我的字体颜色被覆盖。使用<body style='color: ..'>也无效。

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

MetalButtonUI定义了getDisabledTextColor,但实现它并不有效。

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

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

此区域有一个错误修复,据报道已在 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(
               "<html><center>Button<br/>Label</center></html>");      

            // 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

最佳答案

Does anyone know how to control the text color for disabled buttons?

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

enter image description here

enter image description here

enter image description here

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("<html><font color=" + (button.isEnabled() ? "blue" : "red") + ">"
                + buttonText + "</font></html>");
                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();
            }
        });
    }
}

关于Java 7,使用 HTML 格式标签时按钮文本的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10954597/

相关文章:

java - getDeclaringClass 导致未能延迟初始化异常

Java - 关闭UDP套接字

java - 将从 JButton 获得的值传递给变量

java - 正确的 JTextArea 文本环绕

java - JButton 加速器

java - 将功能分配给 Jbutton

java - JPA 添加子记录 - 在父记录中有 3 个副本

java - Javascript 中的 GWT 自定义事件触发器

java - 如何在 JTextPane 中围绕组件包装文本?

java - 用 Java 设计按钮(就像在 CSS 中一样)