实现像网页上的那种用户单击一个Text框然后框下面出现一个日历控件,点击Text框以外的地方日历消失,怎么实现?
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class DateTimeDemo extends Shell
{
private static Display display;
private Text text;
public static void main(String args[])
{
try
{
display = Display.getDefault();
DateTimeDemo shell = new DateTimeDemo(display, SWT.SHELL_TRIM);
shell.open();
shell.layout();
while(!shell.isDisposed())
{
if(!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public DateTimeDemo(Display display, int style)
{
super(display, style);
createContents();
}
protected void createContents()
{
setText("DateTime");
setSize(471, 140);
text = new Text(this, SWT.BORDER);
text.setEditable(false);
text.setBackground(new Color(display, 255, 255, 255));
text.setBounds(122, 41, 228, 25);
text.addMouseListener(new MouseAdapter()
{
public void mouseUp(MouseEvent e)
{
DTDialog dialog = DTDialog.getInstance(DateTimeDemo.this);
dialog.open();
}
});
}
public Point getDtLocation()
{
return new Point(text.getLocation().x + DateTimeDemo.this.getLocation().x,
text.getLocation().y + DateTimeDemo.this.getLocation().y + 60);
}
public void setDate(String str)
{
text.setText(str);
}
@Override
protected void checkSubclass()
{
// Disable the check that prevents subclassing of SWT components
}
}
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;
}
}
分享到:
相关推荐
SWT自带的DateTime控件属实不好用...1、日历控件可自定义设置常见的几种日期格式; 2、可以绑定注册到其他控件如:文本框、按钮、标签上使用; 3、可以绑定注册控件后给指定其他控件赋值。 使用者可以自己再次进行扩展
在这个“SWT日期时间选择控件.rar”压缩包中,包含的是一个针对SWT框架自定义封装的日期和时间选择器控件。这个控件允许用户方便地选取特定的日期和时间,提高了用户界面的交互性和用户体验。 在SWT中,虽然有基础...
`swt-datepicker.jar`是包含这个日期时间选择控件类的库文件,它应该包含了控件的实现代码以及必要的资源。开发者可以通过在他们的项目中引入这个JAR文件,来使用这个功能。通常,引入外部库的方式是在构建路径中...
8. **API接口**: 为了便于其他组件或服务使用这个日期控件,可能会提供一组公共方法,例如`show()`来弹出日历,`selectDate(Date date)`来设置或获取日期等。 开发这样一个控件需要深入理解Java编程,SWT或JFace库...
SWT使用JNI(Java Native Interface)来实现这一目标,它允许Java代码直接调用操作系统提供的API,从而避免了Java AWT和Swing中的“重量级”组件带来的性能问题。 ### 2. 自定义控件的基础 在SWT中,自定义控件通常...
用 SWT 来显示 日历控件 源码 很实用
而"swt实现的日历附带说明使用"这个主题涉及到的是如何在SWT应用中集成日历功能,特别是使用了JDatePicker这个组件。 JDatePicker 是一个基于Java Swing的组件,它提供了日历选择器的功能。虽然SWT主要用在非Swing...
Java使用SWT JFreeChart控件实现的小游戏.zipJava使用SWT JFreeChart控件实现的小游戏.zipJava使用SWT JFreeChart控件实现的小游戏.zipJava使用SWT JFreeChart控件实现的小游戏.zipJava使用SWT JFreeChart控件实现的...
SWT(Standard Widget Toolkit)是Java编程中用于创建图形用户界面(GUI)的一种库,它为开发者提供了丰富的控件和对话框。在SWT中,`MessageBox`是一个用于显示简单的消息对话框的类,通常用来向用户显示警告、确认...
SWT(Standard Widget Toolkit)是Eclipse基金会推出的一种用于创建Java GUI应用程序的开源库,它提供了丰富的用户界面组件,使得开发者能够构建出功能强大的桌面应用。在SWT中,虽然内建了一些基本的控件,但并没有...
在IT行业中,有时候我们需要在Java应用中集成第三方控件或者组件来实现特定的功能,比如在SWT(Standard Widget Toolkit)环境中嵌入Word编辑器。这篇博客"SWT中嵌入Word控件应用"可能就是讲述如何在Java SWT界面中...
**SWT控件详解** SWT(Standard Widget Toolkit)是由Eclipse基金会开发并维护的一套用于构建图形用户界面(GUI)的开源库,它是Java语言中的一个GUI工具包,主要面向那些希望创建高性能、原生外观的应用程序的...
本教程集合了SWT中的所有控件及其使用代码,旨在帮助开发者快速理解和应用这些控件。 1. SWT控件基础 SWT提供了丰富的控件集,包括按钮(Button)、文本框(Text)、列表(List)、表格(Table)、树(Tree)、...
SWT中实现shell Canvas Composite等控件的背景透明
总的来说,这个示例涵盖了SWT GUI开发、时间选择控件的使用、Java定时任务的实现以及通过批处理文件运行Java应用程序的技巧。通过深入学习和理解这些内容,开发者可以创建出具有复杂交互和定时功能的桌面应用程序。
根据提供的文件信息,可以看出本文主要讨论的是如何在 SWT (Standard Widget Toolkit) 的 Table 控件中实现文本换行的功能。SWT 是一个用于开发基于 Java 的桌面应用程序的工具包,它提供了丰富的用户界面组件来帮助...
SWT 使用 OLE 函数调用com控件的资料 SWT 使用 OLE 函数调用com控件的资料SWT 使用 OLE 函数调用com控件的资料SWT 使用 OLE 函数调用com控件的资料 SWT 使用 OLE 函数调用com控件的资料
本文将深入探讨如何使用Draw2D模拟SWT中的RadioButton和CheckedBox控件,以及这些控件在实际应用中的作用和实现方式。 首先,RadioButton和CheckedBox是GUI中的两种常见选择控件。RadioButton通常用于提供一组互斥...
- **绘制机制**:在SWT中,控件的绘制主要是通过监听`PaintListener`接口实现的。当控件需要重新绘制时,系统会自动调用监听器中的`paintControl`方法。 **实践步骤**: 1. **继承Canvas**:通常情况下,自定义控件...