当从另一个 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;
}
}
最佳答案
您的代码有两处错误:
您有两个 SWT 事件循环。你应该只拥有一个。删除第二个。
while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose();
您的新
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/