如何在 Jcalendar 上获取选中的项目

How to get selected item on Jcalendar

我将我的 jCalendar 加载到 Calendar,然后我将日期用作索引,但问题是每个月的日期都不同,所以我不能 select。当我点击 21 时,我 selecting 10.

   Calendar cal = Calendar.getInstance();

   cal.setTime(jCalendar1.getDate());
   int day = cal.get(Calendar.DAY_OF_MONTH);

   JPanel jpanel = jCalendar1.getDayChooser().getDayPanel();
   Component compo[] = jpanel.getComponents();
   compo[day].setBackground(Color.red);
public class CalendarTest2 extends JFrame {
    private static final long serialVersionUID = 1L;

    public CalendarTest2() {
        Calendar cal = Calendar.getInstance();
        JCalendar jCalendar1 = new JCalendar();
        cal.setTime(jCalendar1.getDate());
        int dayToBeSelected = cal.get(Calendar.DAY_OF_MONTH);
        dayToBeSelected = 21;

        JPanel jpanel = jCalendar1.getDayChooser().getDayPanel();
        Component compo[] = jpanel.getComponents();
        for (Component comp : compo) {
            if (!(comp instanceof JButton))
                continue;

            JButton btn = (JButton) comp;
            if (btn.getText().equals(String.valueOf(dayToBeSelected)))
                comp.setBackground(Color.red);
        }
        add(jpanel);
    }

    public static void main(String[] args) {
        CalendarTest2 test = new CalendarTest2();
        test.setVisible(true);
        test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        test.setSize(800, 800);
    }
}

而不是通过index[=21访问要选择的'按钮' =], 尝试通过上面写的 text(day number) 访问按钮。 原因是,使用以 7x7 方式排列的 49 个按钮显示一个月的日历 。 因此,例如)索引 0 将始终指向 'Sunday' 按钮。