Java 7,使用HTML格式标签时按钮文本的颜色 [英] Java 7, color of button text when using HTML formatted labels

查看:58
本文介绍了Java 7,使用HTML格式标签时按钮文本的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a custom UI for certain buttons, implemented by subclassing MetalButtonUI. The buttons use HTML-formatted labels. This is a requirement for me, I need to support multiline button labels.

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

For some reason, when my application runs on Java 7 (scientifically update 4, the most current) the text color when the button is disabled is now grey. This does not happen when running on Java 4 or 6.

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

In the HTML for the button label, I can set the font color by using <font color=..> However this value is ignored when the button is disabled. It seems like somewhere, my font color is overridden when the button is disabled. Using <body style='color: ..'> is also ineffective.

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

I've tried setting Button.disabledText in UIDefaults. This isn't what I really want to do because it affects too many buttons. But in any case it isn't effective for HTML formatted button labels.

MetalButtonUI定义了getDisabledTextColor,但实现无效.

MetalButtonUI defines getDisabledTextColor, but implementing it is not effective.

类似地,实现paintText方法无效.不会为HTML格式的标签调用它.

Similarly, implementing the paintText method is not effective. It is not invoked for HTML-formatted labels.

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

I could override the entire paint method but that seems like an overly complex solution.

此区域中有一个错误修复程序,报告已用Java 7修复,请参见 http ://bugs.sun.com/bugdatabase/view_bug.do?bug_id = 4783068 但是,错误报告对我来说不是很清楚.尚不清楚具体更改了哪些内容,或如何覆盖新行为.

There was a bugfix in this area, reported fixed in Java 7, see http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4783068 The bug report isn't very clear to me however. It isn't clear what specifically was changed, or how to override the new behavior.

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

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

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

Sorry I should have included example code from the get-go. My original code had custom classes for the button and for the UI, with custom paint() methods in the UI class. But I now see that the core problem can be demonstrated very simply, with just a call to button.setForeground(Color.black); In Java 6 this affects the text color for both enabled and disabled states. In Java 7 it affects only the enabled state. mKorbel ... thank you for helping isolate the problem!!!!

    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();
                }
            });
        }
    }

Oracle现在将其作为错误进行了跟踪,请参见 http ://bugs.sun.com/bugdatabase/view_bug.do?bug_id = 7176683

Edit #2: this is now tracked as a bug by Oracle, see http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7176683

推荐答案

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

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

一种方式(您指的是HTML)

one of the way (you meant Html) is

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格式标签时按钮文本的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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