基本的selector接口 日期选择类的核心是Date_selector_panel.仔细分析后,首先做了一些如下变量声明,不要被\\\"常量初始化\\\"语法掷出。
作者:中国IT实验室 来源:中国IT实验室 2007年8月26日
关键字:
全部代码清单还含有少量的的方便组件,使用他们比用当前日期能更好的初始化日历。在这里我没有谈到。
在Date_selector_panel中最值得说的是update_calendar_display()方法,当改变日历的时候该方法更新显示的日历。我使用java.util.Calendar()方法来判断星期日与月初的偏移量并在这些按钮上设置空字符串的标签。最后我将用空字符串来填写表示月末的按钮标签。
通过这种方式,你看到的每个按都在改变,即使它代表的是当前月中的无效天。这里并不需要用代码实现 ,因为当你 将按钮放入GridLayout中,布局便会自动列出你放入的按钮。
[code]private void update_calendar_display()
{
setVisible(false); // Improves paint speed and reduces flicker.
clear_highlight();
// The buttons that comprise the calendar are in a single
// dimensioned array that was added to a 6x7 grid layout in
// order. Because of the linear structure, it's easy to
// lay out the calendar just by changing the labels on
// the buttons. Here's the algorithm used below:
//
// 1) Find out the offset to the first day of the month.
// 2) Clear everything up to that offset.
// 3) Add the days of the month.
// 4) Clear everything else.
int month = calendar.get(Calendar.MONTH);
int year = calendar.get(Calendar.YEAR);
fire_ActionEvent( CHANGE_ACTION, months[month] + " " + year );
calendar.set( year, month, 1 ); // First day of the current month.
int first_day_offset = calendar.get(Calendar.DAY_OF_WEEK); /* 1 */
assert Calendar.SUNDAY == 0;
assert first_day_offset < days.length;
int i = 0;
while( i < first_day_offset-1 ) /* 2 */
days[i++].setText("");
int day_of_month = 1;
for(; i < days.length; ++i ) /* 3 */
{
if( calendar.get(Calendar.MONTH)==today.get(Calendar.MONTH)
&& calendar.get(Calendar.YEAR )==today.get(Calendar.YEAR )
&& calendar.get(Calendar.DATE )==today.get(Calendar.DATE ) )
{ highlight( days[i] );
}
days[i].setText( String.valueOf(day_of_month) );
calendar.roll( Calendar.DATE, /*up=*/ true ); // Forward one day.
day_of_month = calendar.get(Calendar.DATE);
if( day_of_month == 1 )
break;
}
// Note that we break out of the previous loop with i positioned
// at the last day we added, thus the following ++ *must* be a
// preincrement because we want to start clearing at the cell
// a