如何使用SWT制作一个可自动关闭的开机定时器对话框

时间:2018-04-27 00:53:53

标签: java user-interface swt

如何制作以下类型的对话框,其中包含自动倒计时:

enter image description here

目标是让盒子在10秒内需要一个动作(是或否),否则它将恢复为默认状态' no'选项。我甚至不知道从哪里开始为此编写计时器代码,或者如何使对话框自动倒计时

这是我到目前为止所做的:

    File g = new File(pa+"\\data.ini");
    if(g.exists()) {
        Shell shell = new Shell(display, SWT.SHELL_TRIM & (~SWT.RESIZE) & (~SWT.MAX));
        MessageDialog dialog = new MessageDialog(shell, "Detected Recovery File", null,
                "Old Schedule has been found. Recover?", MessageDialog.INFORMATION, new String[] {
                "Yes", "No" }, 0);

        while(true) {
            if(dialog.open()==0) {
                restart=true;
                break;
            }
            break;  
        }           
    }

1 个答案:

答案 0 :(得分:2)

您可以扩展Dialog,然后添加倒计时逻辑。

这是一个具有倒数计时器的简单示例。美学方面,有很多可以改进的,但它符合你的要求:

public static void main(String[] args)
{
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    shell.setText("StackOverflow");

    Button open = new Button(shell, SWT.PUSH);
    open.setText("Open dialog");
    open.addListener(SWT.Selection, e -> new CountdownDialog(shell).open());

    shell.pack();
    shell.open();

    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
        {
            display.sleep();
        }
    }
    display.dispose();
}

private static class CountdownDialog extends Dialog
{
    private static final int COUNTDOWN = 10;

    protected CountdownDialog(Shell parentShell)
    {
        super(parentShell);
    }

    @Override
    protected Control createDialogArea(Composite parent)
    {
        Composite container = (Composite) super.createDialogArea(parent);
        container.setLayout(new GridLayout());

        Label label = new Label(container, SWT.NONE);
        label.setText("Old Schedule has been found. Recover?");
        label.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));

        Label countdown = new Label(container, SWT.NONE);
        countdown.setText(Integer.toString(COUNTDOWN));
        countdown.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));

        new Thread(() -> {
            for (int i = COUNTDOWN - 1; i >= 0; i--)
            {
                try
                {
                    Thread.sleep(1000);
                }
                catch (final InterruptedException e)
                {
                    return;
                }

                if (parent.isDisposed())  // Stop thread when shell is closed
                    break;

                final boolean close = i == 0;
                final String newText = Integer.toString(i);
                Display.getDefault().asyncExec(() -> {
                    if(!countdown.isDisposed())
                        countdown.setText(newText);

                    if (close)
                        CountdownDialog.this.buttonPressed(IDialogConstants.CANCEL_ID);
                });
            }
        }).start();

        return container;
    }

    @Override
    protected void configureShell(Shell newShell)
    {
        super.configureShell(newShell);
        newShell.setText("Detected Recovery File");
    }

    @Override
    protected void createButtonsForButtonBar(Composite parent)
    {
        createButton(parent, IDialogConstants.OK_ID, "Yes", true);
        createButton(parent, IDialogConstants.CANCEL_ID, "No", false);
    }
}