【发布时间】:2013-05-23 01:51:03
【问题描述】:
我正在使用解决方案中的代码在 Add specific background colors to JDaychooser Dates 的 toedter 的 JCalendar 中设置特定日期的颜色。这个解决方案的问题在于它为每个月设置了不同的日期,因为每个月的第一天是不同的。
在我的示例中,我在事件数组列表中添加了 5 月 4 日和 9 月 4 日。+9 从适用于 5 月的那一天开始,但在 9 月它将选择 7,因为该月的第一天从 +6 开始。
我想知道是否有办法获取该月的开始日期,但我似乎无法在 API 文档中找到执行此操作的方法。
这是我的代码:
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);
}
}
【问题讨论】:
-
或者,实现
IDateEvaluator,如图所示here。