SWT实现弹出日历控件

实现像网页上的那种用户单击一个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;
    }
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值