在 SWT 开发中遇到需要在GUI中嵌入浏览器。SWT原生支持嵌入系统自带浏览器,开发采用win开发。系统默认为 IE 但是在开发过程中发现,IE默认最高为IE9,但是系统已经安装为最新的IE11。
开发代码如下:
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTError;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class SwtBrowser {
public static void main(String[] args) {
// TODO Auto-generated method stub
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
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("https://ie.icoa.cn/");
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
打开测试IE版本为:IE9
解决办法:
SWT中无法提高IE版本是因为在windows 中将 JAVA 环境下的IE版本设置了原因需要
1.打开注册表中
HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION
中 javaw.exe 值中9000 更改为 11000 就可以了
在运行上面代码 IE 浏览器就会使最新的版本了。
如果在其中没有javaw.exe和java.exe 就手动创建 DWORD (32位)
如果设置了还是没有起作用请检查IE浏览器中的下图中红色箭头部分未被勾选
最终效果如下图