2014-09-22 68 views
1

我正在使用這個PopupComposite我想知道如何打開彈出式shell(pc1)包含一個按鈕,打開另一個彈出式shell(pc2),而不關閉第一個彈出式shell。我嘗試修改PopupComposite的激活監聽器,但是我得到的只是一個解決方案,每次打開pc2時都會閃爍pc1。我加入shellActivated下面的代碼:如何打開彈出式shell而不關閉前一個

if(shell.getParent() != null) 
      shell.getParent().setVisible(true); 

每當彈出窗口失去他們必須隱藏(我不能使用的FocusListener貝殼,因爲它沒有在Mac上工作)的重點。

這裏是我的測試:

public class TestShells 
{ 
    public static void main(final String[] args) 
    { 
     final Display display = new Display(); 
     final Shell shell = new Shell(display); 
     shell.setLayout(new GridLayout(1, false)); 



     final Composite container = new Composite(shell, SWT.NULL); 
     container.setLayout(new FillLayout()); 

     final Button btn = new Button(container, SWT.PUSH); 
     btn.setText("Button 1"); 

     final PopupComposite pc1 = new PopupComposite(Display.getDefault().getActiveShell(), SWT.NULL); 
     final Button btn2 = new Button(pc1, SWT.PUSH); 
     btn2.setText("Button 2"); 

     final PopupComposite pc2 = new PopupComposite(pc1.getShell(),SWT.NULL); 
     final Text text = new Text(pc2, SWT.BORDER); 

     btn.addSelectionListener(new SelectionListener() 
     { 

      public void widgetSelected(final SelectionEvent e) 
      { 
       pc1.show(btn.getLocation()); 
      } 

      public void widgetDefaultSelected(final SelectionEvent e) 
      { 
      } 
     }); 

     btn2.addSelectionListener(new SelectionListener() 
     { 

      public void widgetSelected(final SelectionEvent e) 
      { 
       pc2.show(btn2.getLocation()); 

      } 

      public void widgetDefaultSelected(final SelectionEvent e) 
      { 
       // TODO Auto-generated method stub 

      } 
     }); 

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

非常感謝您!

回答

1

我不知道如何隱藏上一個窗口,但作爲一個Windows用戶,我也可以告訴你,在一堆程序下面彈出消息常常令人沮喪,所以建議不要這樣做。

至於實際問題:你有沒有試過JDialog?只需在對話窗口中疊加一個JOptionPane即可在按下按鈕時顯示。

看到http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html

編輯:這裏的代碼段應該工作,我沒有測試它。

import org.eclipse.swt.SWT; 
import org.eclipse.swt.widgets.Display; 
import org.eclipse.swt.widgets.Event; 
import org.eclipse.swt.widgets.Listener; 
import org.eclipse.swt.widgets.MessageBox; 
import org.eclipse.swt.widgets.Shell; 

public class Snippet99 { 

    public static void main(String[] args) 
    { 
    final private Display display = new Display(); 
    final private Shell shell = new Shell(display); 
    shell.setLayout(new GridLayout(1, false)); 

    final Composite container = new Composite(shell, SWT.NULL); 
     container.setLayout(new FillLayout()); 

    final private Button btn = 
new Button(container, SWT.PUSH).setText("Button 1"); 
    final private PopupComposite pc1 = 
new PopupComposite(Display.getDefault().getActiveShell(), SWT.NULL); 
    final private Button btn2 = 
new Button(pc1, SWT.PUSH).setText("Button 2"); 
    final private PopupComposite pc2 = 

new PopupComposite(pc1.getShell(),SWT.NULL); 

    private Text text = new Text(pc2, SWT.BORDER); 
    /* 
    only use final if you're setting the value immediately and will never change it again, 
    make sure you set the accessibility of your items (protected/private/public) 
    */ 

    Listener listener = new Listener() { 
     public void handleEvent(Event event) { 
     pc1.show(btn.getLocation()); 
     } 
    }; 

    btn.addListener(SWT.Selection, listener); 

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

謝謝您的建議,但我只允許使用SWT。 – spetrila 2014-09-25 07:38:37

+1

@JNewbie這個變化很重要。在這種情況下,查看修改關閉行爲的示例:http://www.java2s.com/Code/Java/SWT-JFace-Eclipse/Preventashellfromclosingpromptuser.htm – Chrotenise 2014-09-25 08:33:51

+0

已經嘗試過,但我的情況在此時無法驗證因爲我打算下一個打開的外殼,只有在這個關閉之後纔會打開 – spetrila 2014-09-25 11:07:57

相關問題