1

ダイアログシェルとしてJava SWTアプリケーションでシェルを作成しようとしています。「承諾」と「辞退」の2つのボタンがあります。ユーザーが30秒間ボタンをクリックしないと、シェルが自動的に破棄されます。このため、次のコードを試していますが、機能していません。アイデアや提案を使用して助けてください

 public class ServiceRequestDialog extends Dialog {

public ServiceRequestDialog(Shell parent,String nameofrequestor) {
            // Pass the default styles here
            this(parent,SWT.NO_TRIM|SWT.ON_TOP|SWT.DIALOG_TRIM|SWT.APPLICATION_MODAL);
            this.parent=parent;
            nameofRequester=nameofrequestor;
          }

          public ServiceRequestDialog(Shell parent, int style) {
            // Let users override the default styles
            super(parent, style);

          }

          public Shell open() {
          shell  = new Shell(getParent(), getStyle());
          shell.setText(getText());
          shell.setLocation(parent.getLocation().x+190, parent.getLocation().y+215);
          shell.setSize(279, 181);
          shell.setLayout(new FormLayout());
              ......

         shell.open();
         Display display = getParent().getDisplay();
         while (!shell.isDisposed()) {
         if (!display.readAndDispatch()) {
           display.sleep();
          }
       }
       // Return the entered value, or null
       try {
           System.out.println("Thread Sleep");
            Thread.sleep(15000);
            dispose();
        } catch (InterruptedException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
        }
         return shell;
  }

public void dispose(){      
            try {
                if (shell != null) {
                if (shell.isDisposed()==false) {
                shell.dispose();    
                }

                }
            } catch (Exception e) {             
             e.printStackTrace();
            }
        }
}
}
4

1 に答える 1

4

私はあなたに出発点を与えるべきものを自分で実装しました。Dialogを押すと、基本的に JFace が開きますButton。これDialogは、指定された秒数 (例では 5) 後に自動的に閉じます。

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

    Button button = new Button(shell, SWT.PUSH);
    button.setText("Open dialog");

    button.addListener(SWT.Selection, new Listener()
    {
        @Override
        public void handleEvent(Event arg0)
        {
            new MyDialog(shell, 5).open();
        }
    });

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

private static class MyDialog extends Dialog
{
    private int counter = 0;
    private int maxSeconds;

    public MyDialog(Shell parentShell, int maxSeconds)
    {
        super(parentShell);
        this.maxSeconds = maxSeconds;
        setShellStyle(SWT.APPLICATION_MODAL | SWT.CLOSE);
        setBlockOnOpen(true);
    }

    @Override
    protected Control createDialogArea(Composite parent)
    {
        Composite composite = (Composite) super.createDialogArea(parent);

        composite.setLayout(new GridLayout(1, false));

        final Display display = composite.getShell().getDisplay();
        final Label label = new Label(composite, SWT.NONE);
        label.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true));

        /* Set up the timer here */
        final Runnable timer = new Runnable()
        {
            public void run()
            {
                if(!label.isDisposed())
                {
                    label.setText("" + counter++);
                    label.pack();
                    if(counter <= maxSeconds)
                        display.timerExec(1000, this);
                    else
                        MyDialog.this.close();
                }
            }
        };

        /* And start it */
        display.timerExec(0, timer);

        return composite;
    }

    @Override
    protected void configureShell(Shell newShell)
    {
        super.configureShell(newShell);
        newShell.setText("Dialog");
    }
}
于 2013-05-28T13:58:25.807 に答える