繁体   English   中英

更改 JCalendar 背景色

[英]Change JCalendar backgroundcolor

我试图从组件属性本身更改 JCalendar 组件背景颜色,但没有成功。 虽然从组件属性更改前景色工作正常。

我也试过menuCalendar.setBackground(new Color(135,239,251))没有成功。

任何人都可以帮忙吗?

为 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
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM