我将 jCalendar 加载到日历,然后使用该日期作为索引,但问题是每个月的日期都不同,因此我无法选择。当我点击 21 时,我选择了 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);
}
}
不是通过索引访问'要选择的按钮',尝试通过上面写的文本(天数)访问该按钮。原因是,一个月的日历是通过7x7排列的49个按钮来显示的 。因此,例如)索引 0 将始终指向“星期日”按钮。
我是一名优秀的程序员,十分优秀!