2014-09-22 33 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

相关问题