How to use JCalendar to select an element of my array?

巧了我就是萌 提交于 2019-12-12 18:08:40

问题


I have a 3D array that contains 38 years, 12 months, and 31 entries for each month (regardless of how many days in that month). Like so: array[38][12][31]. I also have a JCalendar that is doing nothing now except looking pretty, and the JCalendar has a button underneath. How would I make it so that I can select a date in the calendar, then press the button and it returns the element of my array that would correspond to that date?

Something like

if(buttonPressed){
    year = chosenYear - 1975;
    month = chosenMonth;
    day = chosenDay;

    System.out.print(array[year][month][day]);
}

thanks guys.


回答1:


You can get the selected Date in a PropertyChangeListener, as shown here. Once you have the date, you can get the year, month and day from a Calendar:

Calendar c = Calendar.getInstance();
c.setTime(date);
int y = c.get(Calendar.YEAR);
int m = c.get(Calendar.MONTH);
int d = c.get(Calendar.DAY_OF_MONTH);

Calendar.MONTH is already zero-based, but Calendar.DAY_OF_MONTH is not; and you'll need to adjust the year to your baseline.



来源:https://stackoverflow.com/questions/16645127/how-to-use-jcalendar-to-select-an-element-of-my-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!