我试图从另一个Eclipse浏览器单击按钮时打开一个新的Eclipse浏览器。我正在使用浏览器功能打开将嵌入新浏览器的外壳,但是新外壳正在打开,但页面未加载
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
没有Layout
。向其中添加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;
}
}