【发布时间】:2021-12-16 08:09:34
【问题描述】:
我有一个JCheckbox,这是在JPanel 中,当您选择它时,它应该在JPanel 中显示图像。问题是这样的:正如您在屏幕截图中看到的那样,JCheckbox 的文字由于图像而难以阅读。
我在想是否有某种方法可以将文本与图像进行对比,使文本的颜色与图像相反。
我知道还有其他方法可以修复它,例如将JCheckbox 设置在图像之外,但我必须更改我的程序和结构代码的设计。
例如,这是我想要的外观:
.
这是JCheckBox目前所有的代码,很简单:
final JCheckBox INFO_IMG = new JCheckBox("Ver img");
INFO_IMG.setFont(new Font("Dialog", 0, 12));
INFO_IMG.setBounds(-2, 2, 78, 13);
INFO_IMG.setOpaque(false);
INFO_IMG.setFocusable(false);
INFO_IMG.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(final ItemEvent ie) {
if (1 == ie.getStateChange()) {
INFO_IMG.setText("Ver info");
IMG.setVisible(true);
/* INFO_IMG.setForegound(ROBOT.getPixelColor(
(int)INFO_IMG.getLocationOnScreen.getX() + 12
,(int) INFO_IMG.getLocationOnScreen().getY() + 10));
This is another way that I had thought of,
although it does not work well,would also have to get
the opposite color from the one return.
*/
} else {
INFO_IMG.setText("Ver img");
IMG.setVisible(false);
}
}
});
add(INFO_IMG);
【问题讨论】:
-
你试过
UIManager属性"CheckBox.foreground"吗?如果这不是重复的,请edit 你的问题包括一个minimal reproducible example 来显示你修改的方法。 -
或者,将图像上的文本与
setText("")进行异或运算。 -
您好,我不知道那个属性,请您详细解释一下,谢谢。哦,对不起,现在我添加更多信息。
-
我当然没有想到那个替代方案,我会用它作为最后的手段
-
这里有更多使用
UIManager.put的例子。