实现像网页上的那种用户单击一个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自定义封装的日期时间选择控件,支持选择日期和时间
一个SWT日期时间选择控件类,可方便地加入到自己的代码中,支持拖放
java swt自定义控件java swt自定义控件java swt自定义控件java swt自定义控件
用 SWT 来显示 日历控件 源码 很实用
Java使用SWT JFreeChart控件实现的小游戏.zipJava使用SWT JFreeChart控件实现的小游戏.zipJava使用SWT JFreeChart控件实现的小游戏.zipJava使用SWT JFreeChart控件实现的小游戏.zipJava使用SWT JFreeChart控件实现的...
SWT的MessageBox对话框 SWT有不同类型的对话框。有些对话框具有特殊的属性。 MessageBox messageBox = new MessageBox(shell, SWT.OK|SWT.CANCEL); if (messageBox.open() == SWT.OK) { System.out.println(...
SWT中实现shell Canvas Composite等控件的背景透明
SWT 使用 OLE 函数调用com控件的资料 SWT 使用 OLE 函数调用com控件的资料SWT 使用 OLE 函数调用com控件的资料SWT 使用 OLE 函数调用com控件的资料 SWT 使用 OLE 函数调用com控件的资料
swt table 实现换行
SWT 第三方 date picker RCP
SWT/JFACE的控件库毕竟有限,在应用开发是我们不可避免地要自己开发一些自定义的控件
在使用SWT 的控件CalendarCombo时,点击None之后再选择其他时间,会出现空指针异常,且会导致程序崩溃,改包下的几个类通过改写CalendarCombo解决了这个小bug ,使用方法与CalendarCombo类似,MyCalendarCombo mc=...
解压后调用类JDatePicker(里面有说明和使用)----------------------------------附带jdatepicker.jar
该文档包含swt所有控件的使用代码。大家可以不必为不知道某个控件怎么使用而犯愁。
在swt开发中实现的一个小小的日历对话框,与文本框共同使用的,可以供大家参考,
SWT Table 单元格编辑实例代码
NULL 博文链接:https://liuxiaobin1023-163-com.iteye.com/blog/1144212
java浏览器控件 swt.jar 下载