SWT日历 : 日历 « SWT-JFace-Eclipse « Java

En
Java
1. 图形用户界面
2. 三维图形动画
3. 高级图形
4. 蚂蚁编译
5. Apache类库
6. 统计图
7. 
8. 集合数据结构
9. 数据类型
10. 数据库JDBC
11. 设计模式
12. 开发相关类
13. EJB3
14. 电子邮件
15. 事件
16. 文件输入输出
17. 游戏
18. 泛型
19. GWT
20. Hibernate
21. 本地化
22. J2EE平台
23. 基于J2ME
24. JDK-6
25. JNDI的LDAP
26. JPA
27. JSP技术
28. JSTL
29. 语言基础知识
30. 网络协议
31. PDF格式RTF格式
32. 映射
33. 常规表达式
34. 脚本
35. 安全
36. Servlets
37. Spring
38. Swing组件
39. 图形用户界面
40. SWT-JFace-Eclipse
41. 线程
42. 应用程序
43. Velocity
44. Web服务SOA
45. 可扩展标记语言
Java 教程
Java » SWT-JFace-Eclipse » 日历屏幕截图 
SWT日历
SWT日历

/**
* this is a example for Kalendar dialog for swt dialog.
*
* author: yin_zhiguo
*
* email: yin_zhiguo@hotmail.com
*
*/
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.MouseEvent;

public class KalendarDialog extends Dialog implements MouseListener {

    private Display display = null;
    private Date nowDate = null//current date

    private String selectedDate = null//selected date
    private Shell shell = null;
    private GridLayout gridLayout = null;
    private GridData gridData = null;

    private CLabel sunday = null;
    private CLabel monday = null;
    private CLabel tuesday = null;
    private CLabel wednesday = null;
    private CLabel thursday = null;
    private CLabel friday = null;
    private CLabel saturday = null;

    private Button yearUp = null;
    private Button yearNext = null;
    private Button monthUp = null;
    private Button monthNext = null;
    private CLabel nowLabel = null;

    private CLabel[] days = new CLabel[42];

    public KalendarDialog(Shell parent, int style) {
        super(parent, style);
    }

    public KalendarDialog(Shell parent) {
        this(parent, 0);
    }

    private int getLastDayOfMonth(int year, int month) {
        if (month == ||
            month == ||
            month == ||
            month == ||
            month == ||
            month == 10 ||
            month == 12) {
            return 31;
        }
        if (month == ||
            month == ||
            month == ||
            month == 11) {
            return 30;
        }
        if (month == 2) {
            if (isLeapYear(year)) {
                return 29;
            else {
                return 28;
            }
        }
        return 0;
    }

    public boolean isLeapYear(int year) {
        return (year % == && year % 100 != 0|| (year % 400 == 0);
    }

    private void moveTo(int type, int value) {
        Calendar now = Calendar.getInstance()//get current Calendar object
        now.setTime(nowDate)//set current date
        now.add(type, value)//add to spec time.
        nowDate = new Date(now.getTimeInMillis())//result
        SimpleDateFormat formatter = new 
SimpleDateFormat("yyyy-MM");//format date
        nowLabel.setText(formatter.format(nowDate))//set to label
        setDayForDisplay(now);
    }

    private void setDayForDisplay(Calendar now) {
        int currentDay = now.get(Calendar.DATE);
        now.add(Calendar.DAY_OF_MONTH, -(now.get(Calendar.DATE1))//
        int startIndex = now.get(Calendar.DAY_OF_WEEK1//
        int year = now.get(Calendar.YEAR)//
        int month = now.get(Calendar.MONTH1//
        int lastDay = this.getLastDayOfMonth(year, month)//
        int endIndex = startIndex + lastDay - 1//
        int startday = 1;
        for (int i = 0; i < 42; i++) {
            Color temp = days[i].getBackground();
            if (temp.equals(display.getSystemColor(SWT.COLOR_BLUE))) {
                
days[i].setBackground(display.getSystemColor(SWT.COLOR_WHITE));
            }
        }
        for (int i = 0; i < 42; i++) {
            if (i >= startIndex && i <= endIndex) {
                days[i].setText("" + startday);
                if (startday == currentDay) {
                    
days[i].setBackground(display.getSystemColor(SWT.COLOR_BLUE))//
                }
                startday++;
            else {
                days[i].setText("");
            }
        }

    }

    public void previousYear() {
        moveTo(Calendar.YEAR, -1);
    }

    public void nextYear() {
        moveTo(Calendar.YEAR, 1);
    }

    public void nextMonth() {
        moveTo(Calendar.MONTH, 1);
    }

    public void previousMonth() {
        moveTo(Calendar.MONTH, -1);
    }

    public void mouseDoubleClick(MouseEvent e) {
        CLabel day = (CLabele.getSource();
        if(!day.getText().equals(""))
        {
            this.selectedDate = nowLabel.getText() "-" + day.getText();
            this.shell.close();
        }
    }

    public void mouseDown(MouseEvent e) {}

    public void mouseUp(MouseEvent e) {}


    public Object open() {
        Shell parent = getParent();
        display = Display.getDefault();
        shell = new Shell(parent,  SWT.TITLE |SWT.PRIMARY_MODAL);
        shell.setText("Calendar ver0.02");
        shell.setSize(230220);

        gridLayout = new GridLayout();
        gridLayout.numColumns = 7;
        shell.setLayout(gridLayout);

        gridData = new GridData(GridData.FILL_HORIZONTAL);
        yearUp = new Button(shell, SWT.PUSH | SWT.FLAT);
        yearUp.setText("<");
        yearUp.setLayoutData(gridData);
        yearUp.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                previousYear();
            }
        });

        gridData = new GridData(GridData.FILL_HORIZONTAL);
        monthUp = new Button(shell, SWT.PUSH | SWT.FLAT);
        monthUp.setText("<<");
        monthUp.setLayoutData(gridData);
        monthUp.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                previousMonth();
            }
        });

        nowLabel = new CLabel(shell, SWT.CENTER | SWT.SHADOW_OUT);
        gridData = new GridData(GridData.FILL_HORIZONTAL);
        gridData.horizontalSpan = 3;
        nowLabel.setLayoutData(gridData);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");
        nowLabel.setText(formatter.format(new Date()));

        gridData = new GridData(GridData.FILL_HORIZONTAL);
        monthNext = new Button(shell, SWT.PUSH | SWT.FLAT);
        monthNext.setText(">>");
        monthNext.setLayoutData(gridData);
        monthNext.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                nextMonth();
            }
        });

        gridData = new GridData(GridData.FILL_HORIZONTAL);
        yearNext = new Button(shell, SWT.PUSH | SWT.FLAT);
        yearNext.setText(">");
        yearNext.setLayoutData(gridData);
        yearNext.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                nextYear();
            }
        });

        sunday = new CLabel(shell, SWT.CENTER | SWT.SHADOW_OUT);
        gridData = new GridData(GridData.FILL_HORIZONTAL |
                                GridData.FILL_VERTICAL);
        gridData.widthHint = 20;
        gridData.heightHint = 20;
        sunday.setLayoutData(gridData);
        sunday.setText("Sun");

        monday = new CLabel(shell, SWT.CENTER | SWT.SHADOW_OUT);
        gridData = new GridData(GridData.FILL_HORIZONTAL |
                                GridData.FILL_VERTICAL);
        gridData.widthHint = 20;
        gridData.heightHint = 20;
        monday.setLayoutData(gridData);
        monday.setText("Mon");

        tuesday = new CLabel(shell, SWT.CENTER | SWT.SHADOW_OUT);
        gridData = new GridData(GridData.FILL_HORIZONTAL |
                                GridData.FILL_VERTICAL);
        gridData.widthHint = 20;
        gridData.heightHint = 20;
        tuesday.setLayoutData(gridData);
        tuesday.setText("Tue");

        wednesday = new CLabel(shell, SWT.CENTER | SWT.SHADOW_OUT);
        gridData = new GridData(GridData.FILL_HORIZONTAL |
                                GridData.FILL_VERTICAL);
        gridData.widthHint = 20;
        gridData.heightHint = 20;
        wednesday.setLayoutData(gridData);
        wednesday.setText("Wed");

        thursday = new CLabel(shell, SWT.CENTER | SWT.SHADOW_OUT);
        gridData = new GridData(GridData.FILL_HORIZONTAL |
                                GridData.FILL_VERTICAL);
        gridData.widthHint = 20;
        gridData.heightHint = 20;
        thursday.setLayoutData(gridData);
        thursday.setText("Thu");

        friday = new CLabel(shell, SWT.CENTER | SWT.SHADOW_OUT);
        gridData = new GridData(GridData.FILL_HORIZONTAL |
                                GridData.FILL_VERTICAL);
        gridData.widthHint = 20;
        gridData.heightHint = 20;
        friday.setLayoutData(gridData);
        friday.setText("Fri");

        saturday = new CLabel(shell, SWT.CENTER | SWT.SHADOW_OUT);
        gridData = new GridData(GridData.FILL_HORIZONTAL |
                                GridData.FILL_VERTICAL);
        gridData.widthHint = 20;
        gridData.heightHint = 20;
        saturday.setLayoutData(gridData);
        saturday.setText("Sat");

        for (int i = 0; i < 42; i++) {
            days[inew CLabel(shell, SWT.FLAT | SWT.CENTER);
            gridData = new GridData(GridData.FILL_HORIZONTAL |
                                    GridData.FILL_VERTICAL);
            days[i].setLayoutData(gridData);
            days[i].setBackground(display.getSystemColor(SWT.COLOR_WHITE));
            days[i].addMouseListener(this);
            days[i].setToolTipText("double click get current date.");
        }

        Calendar now = Calendar.getInstance()//
        nowDate = new Date(now.getTimeInMillis());
        setDayForDisplay(now);

        shell.open();
        Display display = parent.getDisplay();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        return selectedDate;
    }

    /**
     @param args
     */
    public static void main(String[] args) {
        Display display = new Display();
        final Shell shell = new Shell(display);
        shell.setText("");

        FillLayout fl = new FillLayout();
        shell.setLayout(fl);
        Button open = new Button(shell, SWT.PUSH);
        open.setText("open");
        open.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
              KalendarDialog cd = new KalendarDialog(shell);
                System.out.println(cd.open());
            }
        });
        shell.pack();
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }
}


           
       
Related examples in the same category
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.