我正在尝试从组件属性本身更改JCalendar组件的背景色,但没有成功。而从组件属性更改前景色则可以正常工作。
我也尝试过menuCalendar.setBackground(new Color(135,239,251)),但没有成功。
有人能帮上忙吗?
发布于 2021-07-14 21:19:08
为JCalendar设置背景颜色非常复杂。有一种方便的方法可以更改日历的部分背景。您可以先看看这是否完成了您想要的操作:menuCalendar.setDecorationBackgroundColor(new Color(135,239,251));
这可能不会达到您想要的效果。可以使用以下代码更改日期按钮后面的背景颜色和按钮本身的颜色。它必须这样做,因为JCalendar使用原生Swing组件,这些组件相互嵌套,似乎缺乏方便的方法:
for (int i = 0; i < menuCalendar.getComponentCount(); i++) {
if (menuCalendar.getComponent(i) instanceof JDayChooser) {
JDayChooser chooser = ((JDayChooser) menuCalendar.getComponent( i ) );
JPanel panel = (JPanel) chooser.getComponent(0);
// the following line changes the color of the background behind the buttons
panel.setBackground(Color.BLACK);
// the for loop below changes the color of the buttons themselves
for (int y = 0; y < panel.getComponentCount(); y++) {
panel.getComponent(y).setBackground(Color.BLACK);
}
break; // leave the for loop, we're done
}
}https://stackoverflow.com/questions/68376639
复制相似问题