首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 企业软件 > 行业软件 >

SWT/JFACE 之年历控件

2012-08-10 
SWT/JFACE 之日历控件修修改改网上的一个日历控件,精确到日期时分秒。视图效果:代码import org.eclipse.swt

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



代码

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();}}@Overridepublic void mouseDoubleClick(MouseEvent e) {// TODO Auto-generated method stub}@Overridepublic void mouseDown(MouseEvent e) {// TODO Auto-generated method stubint 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();}@Overridepublic void mouseUp(MouseEvent e) {// TODO Auto-generated method stub}public void dispose(){if(!this.dialog.isDisposed()){this.dialog.dispose();}}}

热点排行