![](/img/trans.png)
[英]Eclipse SWT browser with XULRunner 10 crashes on Javascript window.print
[英]Linux SWT Browser JavaScript.window.close() method not working
我已经实现了示例swt浏览器应用程序,它可以在Windows操作系统中运行,但是我已经在linux操作系统中测试过的代码相同,浏览器正在打开,但是window.close()
函数在linux中不起作用。 如何解决这个问题?
样例代码
public class AdvancedBrowser
{
public static void main(String[] args)
{
Display display = new Display();
Shell shell = new Shell(display);
Browser browser = new Browser(shell, SWT.NONE);
browser.setBounds(5, 5, 600, 600);
browser.addCloseWindowListener(new CloseWindowListener()
{
public void close(WindowEvent event)
{
System.out.println("closing");
Browser browser = (Browser) event.widget;
Shell shell = browser.getShell();
shell.close();
}
});
browser.setText("<a href=\"javascript:window.close();\">Close this Window</a>");
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
请记住,并非所有浏览器都允许window.close()
。 Internet Explorer(在使用SWT.NONE
时在Windows上使用)允许脚本关闭浏览器窗口(尽管它可能会显示提示)。
Chrome和Firefox(已在Windows和Linux上进行了测试)不允许脚本关闭窗口。
由于您不能在Linux上的SWT中真正使用IE,所以我想不出一种使window.close()
工作的方法。
但是 ,您可以在SWT Browser
从JavaScript调用Java代码:
private static Browser browser;
public static void main(String[] args)
{
Display display = new Display();
Shell shell = new Shell(display);
browser = new Browser(shell, SWT.NONE);
browser.setBounds(5, 5, 600, 600);
browser.addListener(SWT.Close, new Listener()
{
@Override
public void handleEvent(Event event)
{
System.out.println("closing");
Browser browser = (Browser) event.widget;
Shell shell = browser.getShell();
shell.close();
}
});
new CustomFunction(browser, "theJavaFunction");
browser.setText("<a href=\"javascript:theJavaFunction();\">Close this Window</a>");
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
private static 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");
Shell shell = browser.getShell();
shell.close();
return null;
}
}
Vogella 这里有一个不错的教程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.