首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >更改JCalendar背景颜色

更改JCalendar背景颜色
EN

Stack Overflow用户
提问于 2021-07-14 18:48:44
回答 1查看 50关注 0票数 1

我正在尝试从组件属性本身更改JCalendar组件的背景色,但没有成功。而从组件属性更改前景色则可以正常工作。

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

有人能帮上忙吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-07-14 21:19:08

为JCalendar设置背景颜色非常复杂。有一种方便的方法可以更改日历的部分背景。您可以先看看这是否完成了您想要的操作:menuCalendar.setDecorationBackgroundColor(new Color(135,239,251));

这可能不会达到您想要的效果。可以使用以下代码更改日期按钮后面的背景颜色和按钮本身的颜色。它必须这样做,因为JCalendar使用原生Swing组件,这些组件相互嵌套,似乎缺乏方便的方法:

代码语言:javascript
复制
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
    }
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68376639

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档