读书人

SWT兑现弹出日历控件

发布时间: 2012-07-02 17:46:22 作者: rapoo

SWT实现弹出日历控件

实现像网页上的那种用户单击一个Text框然后框下面出现一个日历控件,点击Text框以外的地方日历消失,怎么实现?

    import?org.eclipse.swt.SWT;??import?org.eclipse.swt.events.FocusAdapter;??import?org.eclipse.swt.events.FocusEvent;??import?org.eclipse.swt.events.SelectionAdapter;??import?org.eclipse.swt.events.SelectionEvent;??import?org.eclipse.swt.layout.FillLayout;??import?org.eclipse.swt.widgets.DateTime;??import?org.eclipse.swt.widgets.Dialog;??import?org.eclipse.swt.widgets.Display;??import?org.eclipse.swt.widgets.Shell;????public?class?DTDialog?extends?Dialog??{??????private?Object?result;????????private?Shell?shell;????????private?DateTime?dateTime;????????private?DateTimeDemo?parent;????????private?static?DTDialog?instance;????????public?static?DTDialog?getInstance(DateTimeDemo?parent)??????{??????????if(instance?==?null)??????????{??????????????instance?=?new?DTDialog(parent);??????????}????????????return?instance;??????}????????private?DTDialog(DateTimeDemo?parent)??????{??????????super(parent,?SWT.NO_TRIM);??????????this.parent?=?parent;??????}????????public?Object?open()??????{??????????if(shell?==?null?||?shell.isDisposed())??????????{??????????????createContents();??????????????shell.open();??????????????shell.layout();??????????????Display?display?=?getParent().getDisplay();????????????????while(!shell.isDisposed())??????????????{??????????????????if(!display.readAndDispatch())??????????????????{??????????????????????display.sleep();??????????????????}??????????????}????????????????display.dispose();??????????}??????????else??????????{??????????????shell.setLocation(parent.getDtLocation());??????????????shell.setVisible(true);??????????????shell.setFocus();??????????}????????????return?result;??????}????????protected?void?createContents()??????{??????????shell?=?new?Shell(getParent(),?SWT.NO_TRIM);??????????shell.setLayout(new?FillLayout());??????????shell.setSize(272,?140);??????????shell.setLocation(parent.getDtLocation());??????????shell.setText("SWT?Dialog");??????????dateTime?=?new?DateTime(shell,?SWT.CALENDAR);????????????dateTime.addSelectionListener(new?SelectionAdapter()??????????{??????????????public?void?widgetSelected(SelectionEvent?e)??????????????{??????????????????parent.setDate(formatDt());??????????????}??????????});????????????dateTime.addFocusListener(new?FocusAdapter()??????????{??????????????public?void?focusLost(FocusEvent?e)??????????????{??????????????????parent.setDate(formatDt());??????????????????shell.setVisible(false);??????????????}??????????});??????}????????private?String?formatDt()??????{??????????return?dateTime.getYear()?+?"-"?+?dateTime.getMonth()?+?"-"?+?dateTime.getDay();??????}????????public?Shell?getShell()??????{??????????return?shell;??????}??}?

读书人网 >软件架构设计

热点推荐