`

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;
    }
}
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics