SWT实现弹出日历控件

实现像网页上的那种用户单击一个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(471 140 );  
  51.         text = new  Text( this , SWT.BORDER);  
  52.         text.setEditable(false );  
  53.         text.setBackground(new  Color(display,  255 255 255 ));  
  54.         text.setBounds(122 41 228 25 );  
  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(272 140 );  
  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.     }  

猜你喜欢

转载自marsvaadin.iteye.com/blog/1463176
今日推荐