我目前尝试添加一些带有工具提示的 JCheckBox 实例。只要用户选择或取消选择 JCheckBox(通过 ItemListener),这些工具提示就会发生变化。但是:每次我检查 JCheckbox 时,它都会更改颜色(Alpha),因此它变得不那么透明,所以看起来背景的 Alpha 发生了变化。一旦我等待工具提示,它就会变回来,但这仍然让我有点恼火。
这是我的代码:
public class MyBox extends JCheckBox {
private final String onSelected;
private final String onNotSelected;
private int index;
public MyBox(int index){
super();
this.setDoubleBuffered(true);
this.index = index;
this.onSelected = "ABC";
this.onNotSelected = "DEF";
this.setToolTipText(this.onNotSelected);
super.setOpaque(true);
this.setBackground(new Color(0, 0, 255, 80));
Dimension size = new Dimension(10,10);
this.setHorizontalAlignment(SwingConstants.CENTER);
}
public int getIndex(){
return this.index;
}
public void setTooltipFor(boolean selected){
String setto = selected ? onSelected : onNotSelected;
this.setToolTipText(setto);
}
public void disableEditing(){
this.setEnabled(false);
}
}
和
public class MyListener implements ItemListener{
public MyListener(){
}
@Override
public void itemStateChanged(ItemEvent ie) {
MyBox box =((MyBox) ie.getItemSelectable());
int index = box.getIndex();
if(ie.getStateChange() == ItemEvent.SELECTED){
box.setTooltipFor(true);
}
else{
box.setTooltipFor(false);
}
}
}
Listener 是在另一个类中添加的(增改工程)。问题仍然存在:为什么 alpha 会发生变化(颜色变得更不透明和更暗),我该如何避免这种情况?
最佳答案
But: Everytime I go over the JCheckbox it changes the Color (the Alpha) so it becomes less transparent
是的,Swing 不知道如何正确处理透明颜色,因为您违反了绘画规则。
查看 Backgrounds With Transparency了解更多信息和解决方案。您可以:
- 自己做背景的定制绘画
- 使用提供的
AlphaContainer
类为您绘制
关于java - 颜色(Alpha)通过鼠标悬停而改变,直到出现工具提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36691654/