SWT时间选择器

package com.netunit.workbench.test;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

/**
* Time Picker
* @author 何明
*
*/
public class TimePicker extends Dialog {

protected Object result;
protected Shell shell;

/**
* Create the dialog
*
* @param parent
* @param style
*/
public TimePicker(Shell parent, int style) {
super(parent, style);
}

/**
* Create the dialog
*
* @param parent
*/
public TimePicker(Shell parent) {
this(parent, SWT.NONE);
}

public TimePicker() {
super(Display.getDefault().getActiveShell());
}

/**
* Open the dialog
*
* @return the result
*/
public Object open() {
createContents();
shell.open();
shell.layout();
Display display = getParent().getDisplay();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
return result;
}

/**
* Create contents of the dialog
*/
protected void createContents() {
shell = new Shell(getParent(), SWT.DIALOG_TRIM);
shell.setSize(448, 201);
shell.setText("Time Picker");
shell.setLayout(new GridLayout(3, false));

final DateTime calendar = new DateTime(shell, SWT.CALENDAR | SWT.BORDER);
final DateTime date = new DateTime(shell, SWT.DATE | SWT.SHORT);
final DateTime time = new DateTime(shell, SWT.TIME | SWT.SHORT);

new Label(shell, SWT.NONE);
new Label(shell, SWT.NONE);
Button ok = new Button(shell, SWT.PUSH);
ok.setText(" OK ");
ok.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
ok.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
System.out.println(" Calendar date selected (MM/DD/YYYY) = "
+ (calendar.getMonth() + 1) + " / " + calendar.getDay()
+ " / " + calendar.getYear());
System.out.println(" Date selected (MM/YYYY) = "
+ (date.getMonth() + 1) + " / " + date.getYear());
System.out.println(" Time selected (HH:MM) = "
+ time.getHours() + " : " + time.getMinutes());

System.out.println("(YYYY/MM/DD/HH:MM)" + calendar.getYear()
+ "/" + (calendar.getMonth() + 1 )
+ "/" + calendar.getDay() + "/" + time.getHours() + ":"
+ time.getMinutes());
shell.close();

}

});
shell.setDefaultButton(ok);
//
}

}
在写个main方法测试下就行了
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new FillLayout());

Button open = new Button(shell, SWT.PUSH);
open.setText(" Open Dialog ");
open.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
final TimePicker dialog = new TimePicker();


dialog.open();
}
});
shell.pack();
shell.open();

while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值