欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SWT/JFACE 之日历控件

程序员文章站 2022-05-26 22:08:56
...
修修改改网上的一个日历控件,精确到日期时分秒。
视图效果:

[img]http://dl.iteye.com/upload/attachment/0067/8075/e40ef68b-91b4-3bd5-b09f-651cbec3d87f.png[/img]

代码


import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

/**
*
* @description 日历控件的Dialog
* @className CalendarDialog
* @author share
* @date 2012-5-8 下午2:48:18
*/
public class CalendarDialog extends Dialog implements MouseListener{

private String selectedDate;// 选择的日期
private Display display;
private Shell dialog;
private DateTime calendar;
private DateTime date;
private DateTime time;


public CalendarDialog(Shell parent) {
super(parent);
}

public String getDateText() {
if(selectedDate == null){
return "";
}
return selectedDate.toString();
}

public void open(int x, int y) {
display = Display.getDefault();
dialog = new Shell(getParent(),SWT.DIALOG_TRIM);
dialog.setBounds(x, y, 230, 220);

dialog.setLayout(new GridLayout(3, false));
GridData data = new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL);
data.horizontalSpan = 3;
calendar = new DateTime(dialog, SWT.CALENDAR | SWT.BORDER);
calendar.setLayoutData(data);
date = new DateTime(dialog, SWT.DATE | SWT.SHORT);
time = new DateTime(dialog, SWT.TIME | SWT.SHORT);

Button ok = new Button(dialog, SWT.PUSH);
ok.setText(" OK ");
ok.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));

ok.addMouseListener(this);
dialog.setDefaultButton(ok);
dialog.pack();
dialog.open();

while (!dialog.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}


@Override
public void mouseDoubleClick(MouseEvent e) {
// TODO Auto-generated method stub
}


@Override
public void mouseDown(MouseEvent e) {
// TODO Auto-generated method stub
int month = (calendar.getMonth() + 1);
int day = calendar.getDay();
int hours = time.getHours();
int minutes = time.getMinutes();
selectedDate = calendar.getYear() +"-" + (month<10?"0"+month:month)+
"-" + (day<10?"0"+day:day) + " " + (hours<10?"0"+hours:hours) + ":"+
(minutes<10?"0"+minutes:minutes) + ":00";
this.dialog.close();
}


@Override
public void mouseUp(MouseEvent e) {
// TODO Auto-generated method stub

}

public void dispose(){
if(!this.dialog.isDisposed()){
this.dialog.dispose();
}
}
}