private void initCalPanel() { int year = cal.get(Calendar.YEAR); int month = cal.get(Calendar.MONTH); int maxDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH); int mark = 1; int firstDayOfWeek = new GregorianCalendar(year, month, 1).get(Calendar.DAY_OF_WEEK); panel.setLayout(null); panel.setBackground(Color.WHITE); String[] week = {"日", "一", "二", "三", "四", "五", "六"}; for (int i = 0; i < 7; i++) { addLabelOnPanel(i, week[i], false); } for (int i = 1; i < firstDayOfWeek; i++) { startPoint_x += 40; } for (int i = firstDayOfWeek - 1; i < 7; i++) { addLabelOnPanel(i, "" + mark++, true); } while (mark <= maxDay) { for (int i = 0; i < 7; i++) { addLabelOnPanel(i, "" + mark++, true); if (mark >= maxDay) break; } } }
/** Return the maximum number of days in a month. For example, Feb 2004 has 29 days. */ private int maxDaysInMonth(int year, int month) { Calendar calendar = new GregorianCalendar(year, month, 1); return calendar.getActualMaximum(Calendar.DAY_OF_MONTH); }