【问题标题】:How to open a popup shell without closing the previous one如何在不关闭前一个的情况下打开一个弹出 shell
【发布时间】:2014-11-16 10:33:26
【问题描述】:

我正在使用这个PopupComposite,我想知道如何打开一个包含按钮的弹出式外壳(pc1),该按钮会打开另一个弹出式外壳(pc2),而无需关闭第一个弹出式外壳。我尝试修改 PopupComposite 的激活侦听器,但我得到的只是一个解决方案,每次打开 pc2 时都会闪烁 pc1。我在 shellActivated 中添加了以下代码:

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

每当弹出窗口失去焦点时,它们必须隐藏(我不能将 FocusListener 用于 shell,因为它在 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();
    }
}

非常感谢!

【问题讨论】:

    标签: java shell swt eclipse-rcp jface


    【解决方案1】:

    我不知道如何隐藏上一个窗口,但作为 Windows 用户,我也可以告诉你,在一堆程序下方弹出窗口消失常常令人沮丧,因此建议不要这样做。

    至于实际问题:你试过JDialog吗?只需将JOptionPane 堆叠在按下按钮时将显示的对话框窗口中。

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

    编辑:这是应该可以工作的代码 sn-p,我还没有测试过。

    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();
      }
    }
    

    【讨论】:

    • 感谢您的建议,但我只能使用 SWT。
    • @JNewbie 改变很重要。在这种情况下,看看这个修改关闭行为的例子:java2s.com/Code/Java/SWT-JFace-Eclipse/…
    • 已经试过了,但我的情况无法验证,因为我接下来要打开的外壳,只有在这个关闭后才能打开
    • 那你为什么在你的第一篇文章中声明你想让它们都保持打开状态?
    • 那么我的评论就成立了,你应该看看他的例子,并将你的第二个屏幕的逻辑添加到他给出确认对话框的地方,并修改 actionlistener 以在你按下时显示这个新的弹出窗口一个按钮,而不是关闭屏幕 1。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多