我目前尝试使用ToolTips添加一些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(添加和更改工作)。问题仍然存在:为什么阿尔法变化(颜色变得更不透明和更暗),我该如何避免?
答案 0 :(得分:3)
但是:每次我越过JCheckbox它都会改变颜色(Alpha),使其变得不那么透明
是的,Swing不知道如何正确处理透明色,因为你违反了绘画规则。
查看Backgrounds With Transparency以获取更多信息和解决方案。你可以:
AlphaContainer
课程为您绘画