⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mycalendar.java

📁 用java开发的日历
💻 JAVA
字号:
package calendar;
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.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class MyCalendar extends Dialog implements MouseListener {

	private Display display = null;
	private Shell shell = null;

	private Date nowDate = null;

	private String selectedDate = null;
	private FormLayout formLayout = null;
	private GridData gridData = null;

	private CLabel labelMon = null;
	private CLabel labelTue = null;
	private CLabel labelWed = null;
	private CLabel labelThu = null;
	private CLabel labelFri = null;
	private CLabel labelSat = null;
	private CLabel labelSun = null;

	private Button btnPre = null;
	private Button btnNext = null;
	private Button btnMonthPre = null;
	private Button btnMonthNext = null;
	private Button btnOk = null;
	private Button btnCancel = null;
	private CLabel labelMonth = null;
	private CLabel labelDay = null;
	private CLabel labelYear = null;

	private Composite currentDateComp = null;
	private Composite daysComp = null;

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

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

	public void mouseDoubleClick(MouseEvent arg0) {
		CLabel label = (CLabel) arg0.getSource();

		if (label.getParent().equals(daysComp) && !label.getText().equals("")) {
			this.selectedDate = labelMonth.getText() + " " + label.getText()
					+ " - " + labelYear.getText();
			this.shell.close();
		} else if (label.equals(labelYear)) {
			labelYear.setBackground(display.getSystemColor(SWT.COLOR_GRAY));
			labelDay.setBackground(display
					.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
			labelMonth.setBackground(display
					.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
		} else if (label.equals(labelMonth)) {
			labelMonth.setBackground(display.getSystemColor(SWT.COLOR_GRAY));
			labelDay.setBackground(display
					.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
			labelYear.setBackground(display
					.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
		} else if (label.equals(labelDay)) {
			labelDay.setBackground(display.getSystemColor(SWT.COLOR_GRAY));
			labelMonth.setBackground(display
					.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
			labelYear.setBackground(display
					.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
		}
	}

	public void mouseDown(MouseEvent arg0) {

	}

	public void mouseUp(MouseEvent arg0) {
		CLabel label = (CLabel) arg0.getSource();
		if (label.getParent().equals(daysComp) && !label.getText().equals("")) {
			int targetPos=0, currentPos=0;
			for (int i = 0; i < 42; i++) {
				if (label.equals(days[i])) {
					targetPos = i;
				}
				if (days[i].getBackground().equals(
						display.getSystemColor(SWT.COLOR_GREEN))) {
					currentPos = i;
				}
			}
			moveTo(Calendar.DAY_OF_MONTH, targetPos-currentPos);
		}
	}

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

		FillLayout fillLayout = new FillLayout();
		shell.setLayout(fillLayout);
		Button open = new Button(shell, SWT.PUSH);
		open.setText("Open Mycalendar");
		open.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				MyCalendar llCalendar = new MyCalendar(shell, 0);
				System.out.println(llCalendar.open());
			}
		});

		shell.pack();
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	protected String open() {
		Shell parent = getParent();
		display = Display.getDefault();
		shell = new Shell(parent, SWT.TITLE | SWT.PRIMARY_MODAL);
		shell.setText("MyCalendar");
		shell.setSize(310, 290);

		formLayout = new FormLayout();
		shell.setLayout(formLayout);

		currentDateComp = new Composite(shell, SWT.BORDER);
		FormData fdCurrentDateComp = new FormData(280, 30);
		fdCurrentDateComp.left = new FormAttachment(0, 10);
		fdCurrentDateComp.top = new FormAttachment(0, 5);
		currentDateComp.setLayoutData(fdCurrentDateComp);
		currentDateComp.setLayout(new FormLayout());

		labelMonth = new CLabel(currentDateComp, SWT.CENTER);
		FormData formDataM = new FormData(30, 30);
		formDataM.left = new FormAttachment(0, 5);
		labelMonth.setLayoutData(formDataM);
		SimpleDateFormat formatterM = new SimpleDateFormat("MM -");
		labelMonth.addMouseListener(this);
		labelMonth.setText(formatterM.format(new Date()));

		labelDay = new CLabel(currentDateComp, SWT.CENTER);
		FormData formDataD = new FormData(30, 30);
		formDataD.left = new FormAttachment(0, 35);
		labelDay.setLayoutData(formDataD);
		SimpleDateFormat formatterD = new SimpleDateFormat("dd -");
		labelDay.addMouseListener(this);
		labelDay.setText(formatterD.format(new Date()));

		labelYear = new CLabel(currentDateComp, SWT.CENTER);
		FormData formDataY = new FormData(30, 30);
		formDataY.left = new FormAttachment(0, 65);
		labelYear.setLayoutData(formDataY);
		SimpleDateFormat formatterY = new SimpleDateFormat("yyyy");
		labelYear.addMouseListener(this);
		labelYear.setText(formatterY.format(new Date()));

		btnPre = new Button(currentDateComp, SWT.FLAT | SWT.ARROW | SWT.UP);
		FormData formDataUp = new FormData(15, 15);
		formDataUp.left = new FormAttachment(0, 260);
		formDataUp.top = new FormAttachment(0, 0);
		btnPre.setLayoutData(formDataUp);
		btnPre.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				if (labelYear.getBackground().equals(
						display.getSystemColor(SWT.COLOR_GRAY))) {

					previousYear();
				} else if (labelMonth.getBackground().equals(
						display.getSystemColor(SWT.COLOR_GRAY))) {

					previousMonth();
				} else if (labelDay.getBackground().equals(
						display.getSystemColor(SWT.COLOR_GRAY))) {

					previousDay();
				}
			}
		});
		btnNext = new Button(currentDateComp, SWT.FLAT | SWT.ARROW | SWT.DOWN);
		FormData formDataNext = new FormData(15, 15);
		formDataNext.left = new FormAttachment(0, 260);
		formDataNext.top = new FormAttachment(0, 15);
		btnNext.setLayoutData(formDataNext);
		btnNext.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				if (labelYear.getBackground().equals(
						display.getSystemColor(SWT.COLOR_GRAY))) {
					nextYear();
				} else if (labelMonth.getBackground().equals(
						display.getSystemColor(SWT.COLOR_GRAY))) {
					nextMonth();
				} else if (labelDay.getBackground().equals(
						display.getSystemColor(SWT.COLOR_GRAY))) {
					nextDay();
				}
			}
		});
		Composite daysAndMonBtnComp = new Composite(shell, SWT.BORDER);

		FormData fdDaysAndMonBtnComp = new FormData(280, 170);
		fdDaysAndMonBtnComp.left = new FormAttachment(0, 10);
		fdDaysAndMonBtnComp.top = new FormAttachment(0, 45);
		daysAndMonBtnComp.setLayoutData(fdDaysAndMonBtnComp);
		daysAndMonBtnComp.setLayout(new RowLayout());
		btnMonthPre = new Button(daysAndMonBtnComp, SWT.FLAT | SWT.ARROW | SWT.LEFT);
		btnMonthPre.setLayoutData(new RowData(20, 20));
		btnMonthPre.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				previousMonth();
			}
		});

		daysComp = new Composite(daysAndMonBtnComp, SWT.NONE);
		daysComp.setLayoutData(new RowData(225, 170));
		daysComp.setLayout(new GridLayout(7, true));

		drawDays(labelMon, "Mon");
		drawDays(labelTue, "Tue");
		drawDays(labelWed, "Wed");
		drawDays(labelThu, "Thu");
		drawDays(labelFri, "Fri");
		drawDays(labelSat, "Sat");
		drawDays(labelSun, "Sun");

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

		btnMonthNext = new Button(daysAndMonBtnComp, SWT.FLAT | SWT.ARROW
				| SWT.RIGHT);
		btnMonthNext.setLayoutData(new RowData(20, 20));
		btnMonthNext.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				nextMonth();
			}
		});

		btnOk = new Button(shell, SWT.FLAT);
		FormData fdOK = new FormData(50, 20);
		fdOK.left = new FormAttachment(0, 150);
		fdOK.top = new FormAttachment(0, 230);
		btnOk.setLayoutData(fdOK);
		btnOk.setText("OK");
		btnOk.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				selectedDate = labelMonth.getText() + " " + labelDay.getText()
						+ " " + labelYear.getText();
				shell.close();
			}
		});

		btnCancel = new Button(shell, SWT.FLAT);
		FormData fdCancel = new FormData(50, 20);
		fdCancel.left = new FormAttachment(0, 210);
		fdCancel.top = new FormAttachment(0, 230);
		btnCancel.setLayoutData(fdCancel);
		btnCancel.setText("Cancel");
		btnCancel.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				selectedDate = "";
				shell.close();
			}
		});
		
		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;
	}

	private void drawDays(CLabel day, String dayName) {
		day = new CLabel(daysComp, SWT.CENTER);
		gridData = new GridData(GridData.FILL_HORIZONTAL
				| GridData.FILL_VERTICAL);
		day.setLayoutData(gridData);
		day.setText(dayName);
		day.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
	}

	private int getLastDayOfMonth(int year, int month) {
		if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8
				|| month == 10 || month == 12) {
			return 31;
		}
		if (month == 4 || month == 6 || month == 9 || month == 11) {
			return 30;
		}
		if (month == 2) {
			if (isLeapYear(year)) {
				return 29;
			} else {
				return 28;
			}
		}
		return 0;
	}

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

	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 previousDay() {
		moveTo(Calendar.DAY_OF_MONTH, -1);
	}

	public void nextDay() {
		moveTo(Calendar.DAY_OF_MONTH, 1);
	}

	private void moveTo(int type, int value) {
		Calendar now = Calendar.getInstance();
		now.setTime(nowDate);
		now.add(type, value);
		nowDate = new Date(now.getTimeInMillis());
		SimpleDateFormat formatterM = new SimpleDateFormat("MM -");
		labelMonth.setText(formatterM.format(nowDate));
		SimpleDateFormat formatterD = new SimpleDateFormat("dd -");
		labelDay.setText(formatterD.format(nowDate));
		SimpleDateFormat formatterY = new SimpleDateFormat("yyyy");
		labelYear.setText(formatterY.format(nowDate));
		setDayForDisplay(now);
	}

	private void setDayForDisplay(Calendar now) {
		int currentDay = now.get(Calendar.DATE);
		now.add(Calendar.DAY_OF_MONTH, -(now.get(Calendar.DATE) - 1));
		int startIndex = now.get(Calendar.DAY_OF_WEEK) - 1;
		int year = now.get(Calendar.YEAR);
		int month = now.get(Calendar.MONTH) + 1;
		int lastDay = this.getLastDayOfMonth(year, month);
		if (startIndex == 0) {
			startIndex = 6;
		} else {
			startIndex -= 1;
		}
		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_GREEN))) {

				days[i].setBackground(display
						.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
			}
		}
		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_GREEN));
				}
				startday++;
			} else {
				days[i].setText("");
			}
		}

	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -