打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
SWT实现弹出日历控件
userphoto

2012.01.10

关注

SWT实现弹出日历控件  

2010-08-25 10:01:48|  分类: Eclipse 插件开发 |字号 订阅

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

Java代码
  1. import org.eclipse.swt.SWT;   
  2. import org.eclipse.swt.events.MouseAdapter;   
  3. import org.eclipse.swt.events.MouseEvent;   
  4. import org.eclipse.swt.graphics.Color;   
  5. import org.eclipse.swt.graphics.Point;   
  6. import org.eclipse.swt.widgets.Display;   
  7. import org.eclipse.swt.widgets.Shell;   
  8. import org.eclipse.swt.widgets.Text;   
  9.   
  10. public class DateTimeDemo extends Shell   
  11. {   
  12.     private static Display display;   
  13.   
  14.     private Text text;   
  15.   
  16.     public static void main(String args[])   
  17.     {   
  18.         try  
  19.         {   
  20.             display = Display.getDefault();   
  21.             DateTimeDemo shell = new DateTimeDemo(display, SWT.SHELL_TRIM);   
  22.             shell.open();   
  23.             shell.layout();   
  24.   
  25.             while(!shell.isDisposed())   
  26.             {   
  27.                 if(!display.readAndDispatch())   
  28.                 {   
  29.                     display.sleep();   
  30.                 }   
  31.             }   
  32.   
  33.             display.dispose();   
  34.         }   
  35.         catch(Exception e)   
  36.         {   
  37.             e.printStackTrace();   
  38.         }   
  39.     }   
  40.   
  41.     public DateTimeDemo(Display display, int style)   
  42.     {   
  43.         super(display, style);   
  44.         createContents();   
  45.     }   
  46.   
  47.     protected void createContents()   
  48.     {   
  49.         setText("DateTime");   
  50.         setSize(471140);   
  51.         text = new Text(this, SWT.BORDER);   
  52.         text.setEditable(false);   
  53.         text.setBackground(new Color(display, 255255255));   
  54.         text.setBounds(1224122825);   
  55.   
  56.         text.addMouseListener(new MouseAdapter()   
  57.         {   
  58.             public void mouseUp(MouseEvent e)   
  59.             {   
  60.                 DTDialog dialog = DTDialog.getInstance(DateTimeDemo.this);   
  61.                 dialog.open();   
  62.             }   
  63.         });   
  64.     }   
  65.   
  66.     public Point getDtLocation()   
  67.     {   
  68.         return new Point(text.getLocation().x + DateTimeDemo.this.getLocation().x,   
  69.                         text.getLocation().y + DateTimeDemo.this.getLocation().y + 60);   
  70.     }   
  71.   
  72.     public void setDate(String str)   
  73.     {   
  74.         text.setText(str);   
  75.     }   
  76.   
  77.     @Override  
  78.     protected void checkSubclass()   
  79.     {   
  80.     // Disable the check that prevents subclassing of SWT components   
  81.     }   
  82. }  
 
Java代码
  1. import org.eclipse.swt.SWT;   
  2. import org.eclipse.swt.events.FocusAdapter;   
  3. import org.eclipse.swt.events.FocusEvent;   
  4. import org.eclipse.swt.events.SelectionAdapter;   
  5. import org.eclipse.swt.events.SelectionEvent;   
  6. import org.eclipse.swt.layout.FillLayout;   
  7. import org.eclipse.swt.widgets.DateTime;   
  8. import org.eclipse.swt.widgets.Dialog;   
  9. import org.eclipse.swt.widgets.Display;   
  10. import org.eclipse.swt.widgets.Shell;   
  11.   
  12. public class DTDialog extends Dialog   
  13. {   
  14.     private Object result;   
  15.   
  16.     private Shell shell;   
  17.   
  18.     private DateTime dateTime;   
  19.   
  20.     private DateTimeDemo parent;   
  21.   
  22.     private static DTDialog instance;   
  23.   
  24.     public static DTDialog getInstance(DateTimeDemo parent)   
  25.     {   
  26.         if(instance == null)   
  27.         {   
  28.             instance = new DTDialog(parent);   
  29.         }   
  30.   
  31.         return instance;   
  32.     }   
  33.   
  34.     private DTDialog(DateTimeDemo parent)   
  35.     {   
  36.         super(parent, SWT.NO_TRIM);   
  37.         this.parent = parent;   
  38.     }   
  39.   
  40.     public Object open()   
  41.     {   
  42.         if(shell == null || shell.isDisposed())   
  43.         {   
  44.             createContents();   
  45.             shell.open();   
  46.             shell.layout();   
  47.             Display display = getParent().getDisplay();   
  48.   
  49.             while(!shell.isDisposed())   
  50.             {   
  51.                 if(!display.readAndDispatch())   
  52.                 {   
  53.                     display.sleep();   
  54.                 }   
  55.             }   
  56.   
  57.             display.dispose();   
  58.         }   
  59.         else  
  60.         {   
  61.             shell.setLocation(parent.getDtLocation());   
  62.             shell.setVisible(true);   
  63.             shell.setFocus();   
  64.         }   
  65.   
  66.         return result;   
  67.     }   
  68.   
  69.     protected void createContents()   
  70.     {   
  71.         shell = new Shell(getParent(), SWT.NO_TRIM);   
  72.         shell.setLayout(new FillLayout());   
  73.         shell.setSize(272140);   
  74.         shell.setLocation(parent.getDtLocation());   
  75.         shell.setText("SWT Dialog");   
  76.         dateTime = new DateTime(shell, SWT.CALENDAR);   
  77.   
  78.         dateTime.addSelectionListener(new SelectionAdapter()   
  79.         {   
  80.             public void widgetSelected(SelectionEvent e)   
  81.             {   
  82.                 parent.setDate(formatDt());   
  83.             }   
  84.         });   
  85.   
  86.         dateTime.addFocusListener(new FocusAdapter()   
  87.         {   
  88.             public void focusLost(FocusEvent e)   
  89.             {   
  90.                 parent.setDate(formatDt());   
  91.                 shell.setVisible(false);   
  92.             }   
  93.         });   
  94.     }   
  95.   
  96.     private String formatDt()   
  97.     {   
  98.         return dateTime.getYear() + "-" + dateTime.getMonth() + "-" + dateTime.getDay();   
  99.     }   
  100.   
  101.     public Shell getShell()   
  102.     {   
  103.         return shell;   
  104.     }   
  105. }  
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
SWT复杂托盘程序的实现
使用Eclipse RCP进行桌面程序开发(二):菜单、工具栏和对话框 - 海边沫沫 - ...
一个在windows和基于SWT的java程序间拖拽的例子(外部文件拖曳到SWT)
Java实现类MSN、QQ好友上线通知界面
SWT无边框窗体实现鼠标拖动移动
在RCP中实现系统托盘功能
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服