Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Jcalendar上获取所选项目_Java_Calendar_Jcalendar - Fatal编程技术网

Java 如何在Jcalendar上获取所选项目

Java 如何在Jcalendar上获取所选项目,java,calendar,jcalendar,Java,Calendar,Jcalendar,我将我的jCalendar加载到日历中,然后使用该日期作为索引,但每个月的日期不同,因此我无法选择。当我点击21时,我选择了10 Calendar cal = Calendar.getInstance(); cal.setTime(jCalendar1.getDate()); int day = cal.get(Calendar.DAY_OF_MONTH); JPanel jpanel = jCalendar1.getDayChooser().getDayPanel(

我将我的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);
而不是通过索引访问要选择的“按钮”, 尝试通过写在按钮上的文本(天数)访问按钮。 原因是,一个月的日历使用49个按钮以7x7的方式显示。 因此,对于ex)索引0将始终指向“Sunday”按钮

而不是通过索引访问要选择的“按钮”, 尝试通过写在按钮上的文本(天数)访问按钮。 原因是,一个月的日历使用49个按钮以7x7的方式显示。
因此,对于ex)索引0将始终指向“Sunday”按钮

多谢各位。但我不认为这会得到任何选定日期的索引。我正在给所选的物品上色。哦。。您想更改所选日期按钮的颜色吗?是的,我的意思是,谢谢。但我不认为这会得到任何选定日期的索引。我正在给所选的物品上色。哦。。是否要更改所选日期按钮的颜色?是的,我的意思是
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);
    }
}