我正在使用解决方案中的代码在 toedter 的 JCalendar 中将特定日期的颜色设置为Add specific background colors to JDaychooser Dates。这个解决方案的问题在于它为每个月设置了不同的日期,因为每个月的第一天是不同的。
在我的示例中,我在事件数组列表中添加了 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);
}
}