java - 当我尝试在 BrowserFunction 中使用新浏览器时,空白 Eclipse 窗口打开

标签 java browser swt

当从另一个 Eclipse 浏览器单击该按钮时,我尝试打开一个新的 Eclipse 浏览器。我正在使用 browserfunction 打开 shell,它将嵌入新的浏览器,但新的 shell 正在打开,但页面未加载

import org.eclipse.swt.*;
import org.eclipse.swt.browser.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class Snippet {

  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    shell.setBounds(10, 10, 800, 800);

    final Browser browser;
    try {
        browser = new Browser(shell, SWT.NONE);
    } catch (SWTError e) {
        System.out.println("Could not instantiate Browser: "
                + e.getMessage());
        display.dispose();
        return;
    }
    browser.setUrl("http://localhost:8080/SampleTest");
    final BrowserFunction function = new CustomFunction(browser,
            "getdatafromjava2");

    browser.addProgressListener(new ProgressAdapter() {
        @Override
        public void completed(ProgressEvent event) {
            browser.addLocationListener(new LocationAdapter() {
                @Override
                public void changed(LocationEvent event) {
                    browser.removeLocationListener(this);
                    System.out
                            .println("left java function-aware page, so disposed CustomFunction");
                    function.dispose();
                }
            });
        }
    });

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

class CustomFunction extends BrowserFunction {

CustomFunction(Browser browser, String name) {
    super(browser, name);
}

@Override
public Object function(Object[] arguments) {
    System.out
            .println("theJavaFunction() called from javascript with args:");

    Display display = Display.getCurrent();
    Shell shell = new Shell(display);
    Browser browser2 = new Browser(shell, SWT.NONE);
    // browser2.setText(createHTML ());
    browser2.setUrl("http://localhost:8080/SampleTest/Test.jsp");
    shell.setText("New Window");
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
    return null;
}
}

enter image description here

最佳答案

您的代码有两处错误:

  1. 您有两个 SWT 事件循环。你应该只拥有一个。删除第二个。

    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
    
  2. 您的新 Shell 没有布局。向其中添加一个 FillLayout 将解决您的问题:

    shell.setLayout(new FillLayout());
    

这是一个完整的示例:

public class StackOverflow
{

    public static void main(String[] args)
    {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setLayout(new FillLayout());
        shell.setBounds(10, 10, 800, 800);

        final Browser browser;
        try
        {
            browser = new Browser(shell, SWT.NONE);
        }
        catch (SWTError e)
        {
            System.out.println("Could not instantiate Browser: "
                    + e.getMessage());
            display.dispose();
            return;
        }
        browser.setText("<a href='' onclick='getdatafromjava2()'>Click here</a>");
        final BrowserFunction function = new CustomFunction(browser,
                "getdatafromjava2");

        browser.addProgressListener(new ProgressAdapter()
        {
            @Override
            public void completed(ProgressEvent event)
            {
                browser.addLocationListener(new LocationAdapter()
                {
                    @Override
                    public void changed(LocationEvent event)
                    {
                        browser.removeLocationListener(this);
                        System.out
                                .println("left java function-aware page, so disposed CustomFunction");
                        function.dispose();
                    }
                });
            }
        });

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

class CustomFunction extends BrowserFunction
{

    CustomFunction(Browser browser, String name)
    {
        super(browser, name);
    }

    @Override
    public Object function(Object[] arguments)
    {
        System.out.println("theJavaFunction() called from javascript with args:");

        Display display = Display.getCurrent();
        Shell shell = new Shell(display);
        shell.setLayout(new FillLayout());
        Browser browser2 = new Browser(shell, SWT.NONE);
        // browser2.setText(createHTML ());
        browser2.setText("SUCCESS");
        shell.setText("New Window");
        shell.open();
        return null;
    }
}

关于java - 当我尝试在 BrowserFunction 中使用新浏览器时,空白 Eclipse 窗口打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39711385/

相关文章:

browser - 浏览器编辑控件中的 Tab 键行为

java - 如何增加SWT对话框标题的字体大小

java - 强制 Storm 使用 fat jar 依赖而不是类路径定义的依赖

java - Android,如何使用java从资源id获取xml资源值

Java:将数组数组的字符串表示形式转换为对象列表

php - 如何让浏览器在使用后退按钮时重新发布表单数据?

java - 如何将 SWT 移植到新平台?

java - 在 SWT 中验证文本并尝试清除它但不起作用

java - 以非顺序方式进行 XQuery 迭代