打造完美SWT日期选择控件:(七)日期

来源:互联网 发布:淘宝客qq群推广 编辑:程序博客网 时间:2024/06/05 14:58
日期的要求要简单一点,
1:支持动态布局。
2:支持任意星期开始,不同的星期开始值会影响日期开始的行列位置。
整体效果请看上一篇文章
打造完美SWT日期选择控件:(六)星期的截图。
3:能监听到用户双击事件。并处理。
关键代码如下
    private void setDays() {
        clearDays();
        Calendar cal 
= (Calendar) basicCalendar.clone();
        
int today = cal.get(Calendar.DAY_OF_MONTH);
        
int thisMonth = cal.get(Calendar.MONTH);

        cal.set(Calendar.DAY_OF_MONTH, 
1);
        
int week = cal.get(Calendar.DAY_OF_WEEK);
        week 
-= this.firstDayOfWeek;
        
for (int i = week; i < days.length; cal.add(Calendar.DAY_OF_MONTH, 1)) {
            Label label 
= days[i++];
            
int day = cal.get(Calendar.DAY_OF_MONTH);
            
int month = cal.get(Calendar.MONTH);
            
if (month == thisMonth) {
                
if (day == today) {
                    label.setForeground(getDisplay().getSystemColor(SWT.COLOR_DARK_RED));
                }
                label.setText(Integer.toString(cal.get(Calendar.DAY_OF_MONTH)));
                label.setData(label.getText());
                label.addListener(SWT.MouseDoubleClick, 
this);
            } 
else {
                
return;
            }

        }
    }