Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在SWT中,什么';在父shell和非对话子shell之间进行通信的最佳方式是什么?_Java_Shell_Swt_Communication - Fatal编程技术网

Java 在SWT中,什么';在父shell和非对话子shell之间进行通信的最佳方式是什么?

Java 在SWT中,什么';在父shell和非对话子shell之间进行通信的最佳方式是什么?,java,shell,swt,communication,Java,Shell,Swt,Communication,我有一个用SWT编写的大型现有应用程序需要修改 GUI包含shell打开非对话框子shell 现在我必须在关闭子shell时更新父shell的信息 我设想了两种选择: 将所有子对象转换为扩展对话框类。问题是它需要大量的重构 传递父逻辑类的引用,以便在关闭子逻辑类之前,我可以调用父逻辑类的方法。我不喜欢这个设计 如果在父代码中我可以监听子shell事件并根据子shell上发生的情况采取行动,那就太好了。这是一种可观察的模式。我在“SWT:开发人员笔记本”中读到: 子shell不需要任何事件循环。为

我有一个用SWT编写的大型现有应用程序需要修改

GUI包含shell打开非对话框子shell

现在我必须在关闭子shell时更新父shell的信息

我设想了两种选择:

  • 将所有子对象转换为扩展对话框类。问题是它需要大量的重构
  • 传递父逻辑类的引用,以便在关闭子逻辑类之前,我可以调用父逻辑类的方法。我不喜欢这个设计
  • 如果在父代码中我可以监听子shell事件并根据子shell上发生的情况采取行动,那就太好了。这是一种可观察的模式。我在“SWT:开发人员笔记本”中读到:

    子shell不需要任何事件循环。为什么?因为父shell的事件循环处理在父shell中打开的所有对象的事件分派。子项保持打开状态,直到用户关闭或父项关闭

    我没有在SWT方面做过实验,而且例子也很少。那么SWT是如何做到这一点的呢?我可以为此使用父循环吗


    谢谢。

    我建议您在子shell上使用
    ShellListener
    。然后可以重写
    shellClosed
    方法

    示例

    import org.eclipse.swt.SWT;
    import org.eclipse.swt.events.SelectionAdapter;
    import org.eclipse.swt.events.SelectionEvent;
    import org.eclipse.swt.events.ShellEvent;
    import org.eclipse.swt.events.ShellListener;
    import org.eclipse.swt.layout.GridData;
    import org.eclipse.swt.layout.GridLayout;
    import org.eclipse.swt.widgets.Button;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.Label;
    import org.eclipse.swt.widgets.Shell;
    import org.eclipse.swt.widgets.Text;
    
    public class Test {
    
        private static Text text;
    
        public static void main (String [] args) 
        {
            Display display = new Display ();
            final Shell shell = new Shell (display);
            shell.setLayout(new GridLayout());
            shell.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
            shell.setSize(200, 100);
            shell.setText("Parent Shell");
    
            Label label = new Label(shell, SWT.NONE);
            label.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
            label.setText("The text from child shell ...");
    
            text = new Text(shell, SWT.BORDER);
            text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
    
            Button openChild = new Button(shell, SWT.PUSH);
            openChild.setText("Open Child ...");
            openChild.addSelectionListener(new SelectionAdapter() {
                public void widgetSelected(SelectionEvent e) {
                    openChild(shell);
                }
            });
    
            shell.open ();
    
            while (!shell.isDisposed ()) {
                if (!display.readAndDispatch ()) display.sleep ();
            }
            display.dispose ();
        }
    
        private static void openChild(Shell parent)
        {
            final Shell dialog = new Shell (parent, SWT.DIALOG_TRIM);
            dialog.setLayout(new GridLayout());
            dialog.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
            dialog.setSize(200, 100);
            dialog.setText("Child Shell");
    
            Label childLabel = new Label(dialog, SWT.NONE);
            childLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
            childLabel.setText("Type something here ...");
    
            final Text childText = new Text(dialog, SWT.BORDER);
            childText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
    
            Button okButton = new Button (dialog, SWT.PUSH);
            okButton.setText ("&OK");
            okButton.addSelectionListener(new SelectionAdapter() {
                public void widgetSelected(SelectionEvent e) {
                    dialog.close();
                }
            });
    
    
            dialog.addShellListener(new ShellListener() {
                public void shellIconified(ShellEvent e) {
                }
                public void shellDeiconified(ShellEvent e) {
                }
                public void shellDeactivated(ShellEvent e) {
                }
                public void shellClosed(ShellEvent e) {
                    if(text != null && !text.isDisposed())
                        text.setText(childText.getText());
                }
                public void shellActivated(ShellEvent e) {
                }
            });
    
    
            dialog.setDefaultButton (okButton);
            dialog.open ();
        }
    } 
    
    注意


    您还可以使用
    DisposeListener
    ,但在这种情况下,您不能使用
    text.setText(childText.getText())(参见上面的示例)。要处理此问题,请将文本保存在字符串变量中,然后使用字符串变量填充父文本框。

    我建议您在子shell上使用
    ShellListener
    。然后可以重写
    shellClosed
    方法

    示例

    import org.eclipse.swt.SWT;
    import org.eclipse.swt.events.SelectionAdapter;
    import org.eclipse.swt.events.SelectionEvent;
    import org.eclipse.swt.events.ShellEvent;
    import org.eclipse.swt.events.ShellListener;
    import org.eclipse.swt.layout.GridData;
    import org.eclipse.swt.layout.GridLayout;
    import org.eclipse.swt.widgets.Button;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.Label;
    import org.eclipse.swt.widgets.Shell;
    import org.eclipse.swt.widgets.Text;
    
    public class Test {
    
        private static Text text;
    
        public static void main (String [] args) 
        {
            Display display = new Display ();
            final Shell shell = new Shell (display);
            shell.setLayout(new GridLayout());
            shell.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
            shell.setSize(200, 100);
            shell.setText("Parent Shell");
    
            Label label = new Label(shell, SWT.NONE);
            label.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
            label.setText("The text from child shell ...");
    
            text = new Text(shell, SWT.BORDER);
            text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
    
            Button openChild = new Button(shell, SWT.PUSH);
            openChild.setText("Open Child ...");
            openChild.addSelectionListener(new SelectionAdapter() {
                public void widgetSelected(SelectionEvent e) {
                    openChild(shell);
                }
            });
    
            shell.open ();
    
            while (!shell.isDisposed ()) {
                if (!display.readAndDispatch ()) display.sleep ();
            }
            display.dispose ();
        }
    
        private static void openChild(Shell parent)
        {
            final Shell dialog = new Shell (parent, SWT.DIALOG_TRIM);
            dialog.setLayout(new GridLayout());
            dialog.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
            dialog.setSize(200, 100);
            dialog.setText("Child Shell");
    
            Label childLabel = new Label(dialog, SWT.NONE);
            childLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
            childLabel.setText("Type something here ...");
    
            final Text childText = new Text(dialog, SWT.BORDER);
            childText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
    
            Button okButton = new Button (dialog, SWT.PUSH);
            okButton.setText ("&OK");
            okButton.addSelectionListener(new SelectionAdapter() {
                public void widgetSelected(SelectionEvent e) {
                    dialog.close();
                }
            });
    
    
            dialog.addShellListener(new ShellListener() {
                public void shellIconified(ShellEvent e) {
                }
                public void shellDeiconified(ShellEvent e) {
                }
                public void shellDeactivated(ShellEvent e) {
                }
                public void shellClosed(ShellEvent e) {
                    if(text != null && !text.isDisposed())
                        text.setText(childText.getText());
                }
                public void shellActivated(ShellEvent e) {
                }
            });
    
    
            dialog.setDefaultButton (okButton);
            dialog.open ();
        }
    } 
    
    注意


    您还可以使用
    DisposeListener
    ,但在这种情况下,您不能使用
    text.setText(childText.getText())(参见上面的示例)。要处理此问题,请将文本保存在字符串变量中,然后使用字符串变量填充父文本框。

    谢谢,它的效果非常好。事实上,我需要一个处置者。感谢您的回复,我在SWT方面迈出了一大步。谢谢,它的工作方式很有魅力。事实上,我需要一个处置者。感谢你的回应,我在SWT上迈出了一大步。