简体   繁体   English

JCalendar设置特定的日期颜色

[英]JCalendar set specific date colors

I am using the code from the solution to set the colors of a specific date in toedter's JCalendar at Add specific background colors to JDaychooser Dates . 我正在使用解决方案中的代码在toedter的JCalendar中设置特定日期的颜色, 将特定背景颜色添加到JDaychooser日期 The problem with this solution is that it sets a different day for each month because the first day for each month is different. 此解决方案的问题在于它为每个月设置了不同的日期,因为每个月的第一天是不同的。

in my example i have added 4th of May and 4th of September in the events arraylist.+9 from the day works for May but in September it will select 7 instead because the first day of the month starts at +6. 在我的例子中,我已经在事件arraylist中添加了5月4日和9月4日。从5月份开始的时间为5月,但在9月它将选择7而不是因为该月的第一天从+6开始。

I'm wondering if there is a way to get the start date for the month but i can't seem to find a method that does this in the API documentation. 我想知道是否有办法获得本月的开始日期,但我似乎无法在API文档中找到这样做的方法。

在此输入图像描述

Heres my code: 继承我的代码:

Calendar cal = Calendar.getInstance();
cal.setTime(calendar.getDate());
int day = cal.get(Calendar.DAY_OF_MONTH);
int month = cal.get(Calendar.MONTH);
int year = cal.get(Calendar.YEAR);

JPanel jpanel = calendar.getDayChooser().getDayPanel();
Component component[] = jpanel.getComponents();

//arraylist of events
for(int i = 0; i < events.size(); i++)
{
    //selected month and year on JCalendar
    if(month == events.get(i).getMonth() && year == events.get(i).getYear())
    {
        //this value will differ from each month due to first days of each month
         component[ events.get(i).getDay() + 9 ].setBackground(Color.blue); 
    }
}

What you need is to get the offset of the first day of the month. 您需要的是获得该月第一天的偏移量。 Analysing the calendar you know this is linked with the day of week. 分析您知道的日历与星期几相关联。

Calendar cal = Calendar.getInstance();
cal.setTime(calendar.getDate());
int day = cal.get(Calendar.DAY_OF_MONTH);
int month = cal.get(Calendar.MONTH);
int year = cal.get(Calendar.YEAR);

JPanel jpanel = calendar.getDayChooser().getDayPanel();
Component component[] = jpanel.getComponents();

//arraylist of events
for(int i = 0; i < events.size(); i++)
{
    //selected month and year on JCalendar
    if(month == events.get(i).getMonth() && year == events.get(i).getYear())
    {
         // Calculate the offset of the first day of the month
         cal.set(Calendar.DAY_OF_MONTH,1);
         int offset = cal.get(Calendar.DAY_OF_WEEK) - 1;

        //this value will differ from each month due to first days of each month
         component[ events.get(i).getDay() + offset ].setBackground(Color.blue); 
    }
}

Does that make sense? 那有意义吗?

I added a constant for the seven first objects of panel (Sunday to Saturday) 我为面板的七个第一个对象添加了一个常量(周日到周六)

component[ events.get(i).getDay() + offset + 7].setBackground(Color.blue); 

and it worked for me 它对我有用

Well A Simple Solution is that u have to get each Panel of Dare in Calender then you can easily change its color. 一个简单的解决方案就是你必须在Calender中获得每个Dare面板然后你可以轻松地改变它的颜色。

look following simple Example. 看下面简单的例子。

jPanel2 = jCalendar1.getDayChooser().getDayPanel();
Component component[] = jPanel2.getComponents();


  for (int i = 1; i <8 ; i++) {
         component[i].setBackground(Color.red);
    }

this will help. 这会有所帮助。

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

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