2014-12-13 78 views
0

我更新SWT。如何處置進度條?

這是顯示進度條的代碼。在我的應用程序中點擊「取消」按鈕時,應該運行進度條。這是工作..工作之後,它只是直到只顯示..我的工作後如何殺死這個?

    public static void Pv7(Button ok, Shell shell2) 
        { 
         shell=shell2; 

         final ProgressBar pb3 = new ProgressBar(shell, SWT.SMOOTH); 

         shell.setLayout(new RowLayout()); 
         shell.setSize(1500, 1000); 

         pb3.setSelection(500); 
         pb3.setBounds(100, 40, 200, 20); 

         Label label3 = new Label(shell, SWT.NULL); 
         //label2.setText("Success"); 
         label3.setAlignment(SWT.LEFT); 
         label3.setBounds(10, 40, 80, 20); 
        } 
         __________________ 

       My code is displaying the progress after the process also. How to dispose the progress bar the after the another shell window opened?? 

      This is my main program: 

    import org.eclipse.swt.*; 
    import org.eclipse.swt.events.*; 
    import org.eclipse.swt.graphics.Point; 
    import org.eclipse.swt.graphics.Rectangle; 
    import org.eclipse.swt.layout.*; 
    import org.eclipse.swt.widgets.*; 

    public class Practice1 
    { 
     static Text text = null; 
     final static Shell shell = null; 

     public static void main (String [] args) 
     { 
      final Display display =new Display(); 
      final Shell shell=new Shell(display); 

      RowLayout rowLayout = new RowLayout(); 

      shell.setLayout(rowLayout); 
         shell.setLocation(-1,-7); 
      shell.setBounds(0, 0, 1050, 720); 

      Label label = new Label (shell, SWT.NONE); 
      label.setText ("Enter the text:"); 

      text = new Text (shell, SWT.BORDER); 

      final Button ok = new Button (shell, SWT.PUSH); 
      ok.setText ("OK"); 
      ok.addSelectionListener(new SelectionAdapter() 
      { 
       @Override 
       public void widgetSelected(SelectionEvent e) 
       { 

        PV6.Pv6(ok,shell); 
       } 
      }); 

      Button cancel = new Button (shell, SWT.PUSH); 
      cancel.setText ("Cancel"); 
      cancel.addSelectionListener(new SelectionAdapter() 
      { 
       @Override 
       public void widgetSelected(SelectionEvent e) 
       { 
        System.out.println("Cancel"); 
        System.out.println("User cancelled dialog"); 

        shell.setVisible(false); 
       } 
      }); 
      shell.addListener(SWT.Close, new Listener() 
      { 
        public void handleEvent(Event event) 
       { 
        int style = SWT.APPLICATION_MODAL | SWT.YES | SWT.NO; 
        MessageBox messageBox = new MessageBox(shell, style); 
        messageBox.setText("Information"); 
        messageBox.setMessage("Close the shell?"); 
        event.doit = messageBox.open() == SWT.YES; 
       } 
        }); 

      shell.setDefaultButton (cancel); 

      /*print the ID in the console area**/ 

      ok.addSelectionListener(new SelectionAdapter() 
      { 
       @Override 
       public void widgetSelected(SelectionEvent e) 
       { 
        System.out.println(text.getText()); 
       } 
      }); 


      //Creating another shell 

      ok.addSelectionListener (new SelectionAdapter() 
      { 

       @Override 
       public void widgetSelected (SelectionEvent e) 
       { 
              System.out.println(text.getText()); 


        final Shell dialog = new Shell (shell, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL); 
        dialog.setText("Dialog Shell"); 
        dialog.setBounds(50,100, 10, 10); 
        FormLayout formLayout = new FormLayout(); 
        formLayout.marginWidth = 10; 
        formLayout.marginHeight = 10; 

        dialog.setLayout (formLayout); 

        Label label = new Label (dialog, SWT.NONE); 
        label.setText ("Type a Name:"); 
        FormData data = new FormData(); 
        label.setLayoutData (data); 

        Button cancel = new Button (dialog, SWT.PUSH); 
        cancel.setText ("Cancel"); 

        data = new FormData(); 
        data.width = 60; 
        data.right = new FormAttachment (100, 0); 
        data.bottom = new FormAttachment (100, 0); 

        cancel.setLayoutData (data); 
        cancel.addSelectionListener (new SelectionAdapter() 
        { 
         @Override 
         public void widgetSelected (SelectionEvent e) 
         { 

          System.out.println("User cancelled dialog"); 
          dialog.close(); 
         } 
        }); 

        text = new Text (dialog, SWT.BORDER); 
        data = new FormData(); 
        data.width = 200; 
        data.left = new FormAttachment (label, 0, SWT.DEFAULT); 
        data.right = new FormAttachment (100, 0); 
        data.top = new FormAttachment (label, 0, SWT.CENTER); 
        data.bottom = new FormAttachment (cancel, 0, SWT.DEFAULT); 
        text.setLayoutData (data); 

        final Button ok = new Button (dialog, SWT.PUSH); 
        ok.setText ("OK"); 
        data = new FormData(); 
        data.width = 60; 
        data.right = new FormAttachment (cancel, 0, SWT.DEFAULT); 
        data.bottom = new FormAttachment (100, 0); 
        ok.setLayoutData (data); 

        ok.addSelectionListener (new SelectionAdapter() 
        { 
         @Override 
         public void widgetSelected (SelectionEvent e) 
         { 
          System.out.println (" " + text.getText()); 
          PV6.Pv6(ok,shell); 
          // dialog.close(); 
         } 
        }); 

        //Creating another shell 

        ok.addSelectionListener (new SelectionAdapter() 
        { 

         @Override 
         public void widgetSelected (SelectionEvent e) 
         { 
          //PV6.Pv6(ok,shell); 
          System.out.println(text.getText()); 


          final Shell dialog = new Shell (shell, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL); 
          dialog.setText("Dialog Shell"); 
          FormLayout formLayout = new FormLayout(); 
          formLayout.marginWidth = 50; 
          formLayout.marginHeight = 10; 
          formLayout.spacing = 100; 

          dialog.setLayout (formLayout); 

          Label label = new Label (dialog, SWT.NONE); 
          label.setText ("Type a Name:"); 
          FormData data = new FormData(); 
          label.setLayoutData (data); 

          Button cancel = new Button (dialog, SWT.PUSH); 
          cancel.setText ("Cancel"); 

          data = new FormData(); 
          data.width = 60; 
          data.right = new FormAttachment (100, 0); 
          data.bottom = new FormAttachment (100, 0); 

          cancel.setLayoutData (data); 
          cancel.addSelectionListener (new SelectionAdapter() 
          { 
           @Override 
           public void widgetSelected (SelectionEvent e) 
           { 

            System.out.println("User cancelled dialog"); 
            dialog.close(); 
           } 
          }); 

          text = new Text (dialog, SWT.BORDER); 
          data = new FormData(); 
          data.width = 200; 
          data.left = new FormAttachment (label, 0, SWT.DEFAULT); 
          data.right = new FormAttachment (100, 0); 
          data.top = new FormAttachment (label, 0, SWT.CENTER); 
          data.bottom = new FormAttachment (cancel, 0, SWT.DEFAULT); 
          text.setLayoutData (data); 

          final Button ok = new Button (dialog, SWT.PUSH); 
          ok.setText ("OK"); 
          data = new FormData(); 
          data.width = 60; 
          data.right = new FormAttachment (cancel, 0, SWT.DEFAULT); 
          data.bottom = new FormAttachment (100, 0); 
          ok.setLayoutData (data); 

          ok.addSelectionListener (new SelectionAdapter() 
          { 
           @Override 
           public void widgetSelected (SelectionEvent e) 
           { 
            System.out.println ("User typed: " + text.getText()); 

            PV6.Pv6(ok,shell); 
            // dialog.close(); 
           } 
          });       dialog.setDefaultButton (ok); 
          dialog.pack(); 
          dialog.open(); 
         } 
        }); 

        dialog.setDefaultButton (ok); 
        dialog.pack(); 
        dialog.open(); 

        // Move the dialog to the center of the top level shell. 
        Rectangle shellBounds = shell.getBounds(); 
        Point dialogSize = dialog.getSize(); 

        //shell.setBounds(0, 0, 1050, 720); 

        dialog.setLocation( 
          shellBounds.x + (shellBounds.width - dialogSize.x)/2, 
          shellBounds.y + (shellBounds.height - dialogSize.y)/2); 
       } 
      }); 
      shell.pack(); 
      shell.open(); 

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

       Any one help me for dispose the progress bar?? 
+0

正在完成的「工作」的代碼在哪裏? – Baz 2014-12-13 13:06:59

+0

import org.eclipse.swt。*; import org.eclipse.swt.events。*; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.layout。*; import org.eclipse.swt.widgets。*; public class Practice1 { \t static Text text = null; \t final static Shell shell = null; \t公共靜態無效的主要(字串[] args) \t { \t \t最終顯示顯示=新顯示(); \t \t final Shell shell = new Shell(display); RowLayout rowLayout = new RowLayout(); shell.setLayout(rowLayout); shell.setLocation(-1,-7); \t \t shell.setBounds(0,0,1050,720); – afu 2014-12-13 13:47:40

+0

Label label = new Label(shell,SWT.NONE); \t \t label.setText(「Enter the text:」); text = new Text(shell,SWT.BORDER); final Button ok = new Button(shell,SWT.PUSH); \t \t ok.setText(「OK」); \t \t ok.addSelectionListener(新SelectionAdapter() \t \t { \t \t \t @Override \t \t \t公共無效widgetSelected(SelectionEvent E) \t \t \t { \t \t \t \t PV6.Pv6(確定,殼); \t \t \t} \t \t}); shell.pack(); \t \t shell.open(); (!shell.isDisposed())(!display.readAndDispatch()) 而 \t \t { \t \t \t如果display.sleep(); \t \t} \t \t顯示。dispose(); \t} } – afu 2014-12-13 13:49:14

回答

0

我還不能說我完全理解你的問題。我不知道什麼時候你要處置ProgressBar,但我只是解釋如何做到這一點,那麼你決定何時調用該方法:

private static Shell shell; 
private static ProgressBar bar; 

public static void Pv7(Button ok, Shell shell2) 
{ 
    shell=shell2; 

    if(bar != null && !bar.isDisposed()) 
     bar.dispose(); 

    bar = new ProgressBar(shell, SWT.SMOOTH); 

    shell.setLayout(new RowLayout()); 
    shell.setSize(1500, 1000); 

    pb3.setSelection(500); 

    Label label3 = new Label(shell, SWT.NULL); 
    label3.setAlignment(SWT.LEFT); 
} 

public static void disposeProgressBar() 
{ 
    if(bar != null && !bar.idDisposed()) 
     bar.dispose(); 
} 

然後,只需調用disposeProgressBar()當你想ProgressBar待處置。

+0

謝謝巴茲你的答案。如果第二個窗口開始顯示,那麼進度條應該處理。我想我在錯誤的地方調用這個方法?在哪裏調用這個方法? – afu 2014-12-15 11:41:54

+0

@AfshanAhamed因爲我還不明白你實際上想做什麼,我無法真正幫助你。如果您可以創建一個[最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)(我可以複製/粘貼並運行),並描述期望的結果是什麼以及爲什麼當前結果不是你想要的,那麼我可能會幫助你。 – Baz 2014-12-15 11:45:07

+0

一旦ok按鈕被點擊,另一個窗口開始顯示..因爲我必須顯示進度條..進度條顯示..到這個沒問題..當我開始在第二個窗口工作後,進度條應該不可見..我的代碼沒有隱藏進度條之後的目的。在這裏我無法將圖像粘貼到我的輸出中。請問您可以給您的郵件ID嗎? – afu 2014-12-15 11:49:48