我有一个带有一些文本的JRadioButton,包含html代码.当我将其设置为禁用时,文本颜色不会更改为灰色或其他内容.如何将其设置为默认禁用的组件文本颜色?我可以直接在文本中设置文字颜色,如:
<html><font color=someColor>...
Run Code Online (Sandbox Code Playgroud)
但是如何获取禁用的组件文本的默认颜色?此外,我试图覆盖绘制方法,并使用这样的东西:
Graphics2D g2 = (Graphics2D) g.create();
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.35f));
super.paintComponent(g2);
g2.dispose();
Run Code Online (Sandbox Code Playgroud)
但我没有得到预期的结果 - 它变成灰色,但与默认禁用的组件文本颜色不同.
因此,解决方案可能是从UIManager.getColor("ComboBox.disabledForeground")获取禁用的颜色; 导致此属性在所有Os中可用.以下是代码:
import javax.swing.*;
import java.awt.*;
public class HTMLJRadio extends JRadioButton {
static String prefixEnabled = "<html><body style='color: black;'>";
String text;
String prefixDisabled;
HTMLJRadio(String text) {
super(prefixEnabled + text);
this.text = text;
Color c = UIManager.getColor("ComboBox.disabledForeground");
String color = Integer.toHexString(c.getRed()) +
Integer.toHexString(c.getGreen()) +
Integer.toHexString(c.getBlue());
prefixDisabled = "<html><body style='color: #" + color + ";'>";
}
public void setEnabled(boolean enabled) {
super.setEnabled(enabled);
if (enabled) {
setText(prefixEnabled + text);
} else {
setText(prefixDisabled + text);
}
}
public static void showButtons() {
String htmlText = "<h1>Laf</h1><p>Ha Ha!";
JPanel p = new JPanel(new GridLayout(0, 1, 3, 3));
HTMLJRadio button1 = new HTMLJRadio(htmlText);
p.add(button1);
HTMLJRadio button2 = new HTMLJRadio(htmlText);
button2.setEnabled(false);
p.add(button2);
JRadioButton button3 = new JRadioButton("Non html disabled");
button3.setEnabled(false);
p.add(button3);
JOptionPane.showMessageDialog(null, p);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
}
showButtons();
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
在ubuntu上它看起来像:
所以带有html内部的禁用单选按钮看起来非常类似于没有内置html的禁用单选按钮.此外,你可以使用答案的解决方案,与图像有一些魔力.
编辑:(按AT)以前这个问题被标记为'正确'.通过双方协议,OP撤回了正确的标记,因为提供的答案不包括屏幕截图中显示的细微差别.特别是下方按钮中文本周围的"浮雕"效果,使其与禁用的HTML格式按钮区分开来.
关于如何实现这种效果的进一步建议将得到(我们双方)的赞赏.
...另请注意,当禁用某个按钮时,其HTML文本仍然是黑色,而不是变为灰色.(请参阅错误#4783068以查看此情况是否发生变化.)
也许你可以从这样的事情开始.
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
class HTMLButton extends JButton {
static String prefixEnabled = "<html><body style='color: black;'>";
String text;
String prefixDisabled;
HTMLButton(String text) {
super(prefixEnabled + text);
this.text = text;
Color c = determineDisabledColorByWitchCraft();
//UIManager.getColor("Button.disabledText");
String color =
Integer.toHexString(c.getRed()) +
Integer.toHexString(c.getGreen()) +
Integer.toHexString(c.getBlue());
prefixDisabled = "<html><body style='color: #" + color + ";'>";
}
private static String getHex(int n) {
return Integer.toHexString(n);
}
public void setEnabled(boolean enabled) {
super.setEnabled(enabled);
if (enabled) {
setText(prefixEnabled + text);
} else {
setText(prefixDisabled + text);
}
}
public static Color determineDisabledColorByWitchCraft() {
// this is little 'block' character..
JButton b = new JButton(String.valueOf('\u2586'));
b.setSize(b.getPreferredSize());
b.setEnabled(false);
BufferedImage biDisabled = new BufferedImage(
b.getWidth(),
b.getHeight(),
BufferedImage.TYPE_INT_RGB);
Graphics g2 = biDisabled.getGraphics();
b.paint(g2);
// get the middle pixel..
int x = b.getWidth()/2;
int y = b.getHeight()/2;
return new Color(biDisabled.getRGB(x,y));
}
public static void showButtons() {
String htmlText = "<h1>Laf</h1><p>Ha Ha!";
JPanel p = new JPanel(new GridLayout(0,1,3,3));
HTMLButton button1 = new HTMLButton(htmlText);
p.add(button1);
HTMLButton button2 = new HTMLButton(htmlText);
button2.setEnabled(false);
p.add(button2);
JButton button3 = new JButton("Hi there!");
button3.setEnabled(false);
p.add(button3);
JOptionPane.showMessageDialog(null, p);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
showButtons();
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch(Exception e) {
}
showButtons();
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
(失败)尝试使用CSS定位获得效果.只是想到我会报告最新的失败,为了拯救其他人而烦恼,想知道这是否足够.
这个HTML:
<html>
<head>
<style type='text/css'>
body {
font-size: 16px;
}
.main {
position: fixed;
top: -16px;
left: 0px;
color: black;
}
.emboss {
position: fixed;
top: 0px;
left: 1px;
color: red;
}
</style>
</head>
<body>
<p class='emboss'><b>The quick brown fox jumped over the lazy dog.</b>
<p class='main'><b>The quick brown fox jumped over the lazy dog.</b>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
在浏览器(例如FF)中呈现的内容非常类似:
.. JEditorPane
像这样:
:-(