`
snoopy7713
  • 浏览: 1158180 次
  • 性别: Icon_minigender_2
  • 来自: 火星郊区
博客专栏
Group-logo
OSGi
浏览量:0
社区版块
存档分类
最新评论

SWT实现弹出日历控件

    博客分类:
  • RAP
阅读更多

实现像网页上的那种用户单击一个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.     }  

分享到:
评论

相关推荐

    SWT/JFace 自定义日历控件(可绑定注册到其他控件)

    SWT自带的DateTime控件属实不好用...1、日历控件可自定义设置常见的几种日期格式; 2、可以绑定注册到其他控件如:文本框、按钮、标签上使用; 3、可以绑定注册控件后给指定其他控件赋值。 使用者可以自己再次进行扩展

    SWT日期时间选择控件.rar

    在这个“SWT日期时间选择控件.rar”压缩包中,包含的是一个针对SWT框架自定义封装的日期和时间选择器控件。这个控件允许用户方便地选取特定的日期和时间,提高了用户界面的交互性和用户体验。 在SWT中,虽然有基础...

    一个SWT日期时间选择控件类

    `swt-datepicker.jar`是包含这个日期时间选择控件类的库文件,它应该包含了控件的实现代码以及必要的资源。开发者可以通过在他们的项目中引入这个JAR文件,来使用这个功能。通常,引入外部库的方式是在构建路径中...

    RCP弹出日期控件

    8. **API接口**: 为了便于其他组件或服务使用这个日期控件,可能会提供一组公共方法,例如`show()`来弹出日历,`selectDate(Date date)`来设置或获取日期等。 开发这样一个控件需要深入理解Java编程,SWT或JFace库...

    java swt自定义控件

    SWT使用JNI(Java Native Interface)来实现这一目标,它允许Java代码直接调用操作系统提供的API,从而避免了Java AWT和Swing中的“重量级”组件带来的性能问题。 ### 2. 自定义控件的基础 在SWT中,自定义控件通常...

    SWT 实现日历空间

    用 SWT 来显示 日历控件 源码 很实用

    swt实现的日历附带说明使用

    而"swt实现的日历附带说明使用"这个主题涉及到的是如何在SWT应用中集成日历功能,特别是使用了JDatePicker这个组件。 JDatePicker 是一个基于Java Swing的组件,它提供了日历选择器的功能。虽然SWT主要用在非Swing...

    Java使用SWT JFreeChart控件实现的小游戏.zip

    Java使用SWT JFreeChart控件实现的小游戏.zipJava使用SWT JFreeChart控件实现的小游戏.zipJava使用SWT JFreeChart控件实现的小游戏.zipJava使用SWT JFreeChart控件实现的小游戏.zipJava使用SWT JFreeChart控件实现的...

    SWT 弹出对话框 可以选择的图标种类

    SWT(Standard Widget Toolkit)是Java编程中用于创建图形用户界面(GUI)的一种库,它为开发者提供了丰富的控件和对话框。在SWT中,`MessageBox`是一个用于显示简单的消息对话框的类,通常用来向用户显示警告、确认...

    SWT日期控件

    SWT(Standard Widget Toolkit)是Eclipse基金会推出的一种用于创建Java GUI应用程序的开源库,它提供了丰富的用户界面组件,使得开发者能够构建出功能强大的桌面应用。在SWT中,虽然内建了一些基本的控件,但并没有...

    SWT 中嵌入Word 控件应用

    在IT行业中,有时候我们需要在Java应用中集成第三方控件或者组件来实现特定的功能,比如在SWT(Standard Widget Toolkit)环境中嵌入Word编辑器。这篇博客"SWT中嵌入Word控件应用"可能就是讲述如何在Java SWT界面中...

    SWT控件

    **SWT控件详解** SWT(Standard Widget Toolkit)是由Eclipse基金会开发并维护的一套用于构建图形用户界面(GUI)的开源库,它是Java语言中的一个GUI工具包,主要面向那些希望创建高性能、原生外观的应用程序的...

    SWT基础代码涵盖其所有控件

    本教程集合了SWT中的所有控件及其使用代码,旨在帮助开发者快速理解和应用这些控件。 1. SWT控件基础 SWT提供了丰富的控件集,包括按钮(Button)、文本框(Text)、列表(List)、表格(Table)、树(Tree)、...

    SWT中实现shell Canvas Composite等控件的背景透明

    SWT中实现shell Canvas Composite等控件的背景透明

    SWT 时间控件及执行bat文件

    总的来说,这个示例涵盖了SWT GUI开发、时间选择控件的使用、Java定时任务的实现以及通过批处理文件运行Java应用程序的技巧。通过深入学习和理解这些内容,开发者可以创建出具有复杂交互和定时功能的桌面应用程序。

    swt table 实现换行

    根据提供的文件信息,可以看出本文主要讨论的是如何在 SWT (Standard Widget Toolkit) 的 Table 控件中实现文本换行的功能。SWT 是一个用于开发基于 Java 的桌面应用程序的工具包,它提供了丰富的用户界面组件来帮助...

    SWT 使用 OLE 函数调用com控件的资料

    SWT 使用 OLE 函数调用com控件的资料 SWT 使用 OLE 函数调用com控件的资料SWT 使用 OLE 函数调用com控件的资料SWT 使用 OLE 函数调用com控件的资料 SWT 使用 OLE 函数调用com控件的资料

    Draw2D 模拟SWT控件之RadioButton、CheckedBox

    本文将深入探讨如何使用Draw2D模拟SWT中的RadioButton和CheckedBox控件,以及这些控件在实际应用中的作用和实现方式。 首先,RadioButton和CheckedBox是GUI中的两种常见选择控件。RadioButton通常用于提供一组互斥...

    SWT 自定义控件

    - **绘制机制**:在SWT中,控件的绘制主要是通过监听`PaintListener`接口实现的。当控件需要重新绘制时,系统会自动调用监听器中的`paintControl`方法。 **实践步骤**: 1. **继承Canvas**:通常情况下,自定义控件...

Global site tag (gtag.js) - Google Analytics