java jbutton 文字颜色,Java 7,使用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.

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.

In the HTML for the button label, I can set the font color by using 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 is also ineffective.

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 defines getDisabledTextColor, but implementing it is not effective.

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.

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?

EDIT: 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(

"

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

}

});

}

}

解决方案

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

one of the way (you meant Html) is

LAPlE.jpg

1W6Q3.jpg

iIc73.jpg

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

}

});

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值