论坛首页 Java企业应用论坛

SWT时间选择器

浏览 3624 次
该帖已经被评为隐藏帖
作者 正文
   发表时间:2009-09-04   最后修改:2009-11-26
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();
	}
   发表时间:2010-01-11  
非常好。谢谢。
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics