SWT/JFACE 之日历控件

修修改改网上的一个日历控件,精确到日期时分秒。
视图效果:

[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();
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值