在JAVA FX中使用JxBrowser,启动之后打开网页,如何这个时候,直接关了窗口,Java进程并不会立马就停掉,而是会等一会,这就是由于JxBrowser还没有关闭导致的。
只要执行以下代码就可以关闭JxBrowser:
new Thread(new Runnable() {
@Override
public void run() {
browser.dispose();
}
}).start();
可参考官方的文章,https://jxbrowser.support.teamdev.com/support/solutions/articles/9000013069-lightweight-or-heavyweight然后以适当的方式关闭它。
public class Util {
public enum OS {
WINDOWS, LINUX, MAC, SOLARIS
}// Operating systems.
private static OS os = null;
private static final String OPERATING_SYSTEM = System.getProperty("os.name").toLowerCase();
public static OS getOS() {
if (os == null) {
if (OPERATING_SYSTEM.contains("win"))
os = OS.WINDOWS;
else if (OPERATING_SYSTEM.contains("nix") || OPERATING_SYSTEM.contains("nux") || OPERATING_SYSTEM.contains("aix")) {
os = OS.LINUX;
} else if (OPERATING_SYSTEM.contains("mac"))
os = OS.MAC;
else if (OPERATING_SYSTEM.contains("sunos"))
os = OS.SOLARIS;
}
return os;
}
public static boolean isWindows() {
return OPERATING_SYSTEM.contains("win");
}
public static boolean isLinux() {
return ( OPERATING_SYSTEM.contains("nix") || OPERATING_SYSTEM.contains("nux") || OPERATING_SYSTEM.contains("aix") );
}
public static boolean isMac() {
return OPERATING_SYSTEM.contains("mac");
}
public static boolean isSolaris() {
return OPERATING_SYSTEM.contains("sunos");
}
/**
* Exit Application
*
* @param code
*/
public static void exitApplication(int code) {
System.out.println("Dis All->" + Util.getOS());
switch (Util.getOS()) {
case WINDOWS:
new Thread(() -> {
//->dispose all the browsers
code....
System.exit(code);
}).start();
break;
case LINUX:
case MAC:
Platform.runLater(() -> {
//->dispose all the browsers
code.....
System.exit(code);
});
break;
default:
System.out.println("Can't dispose browser instance!!!");
break;
}
}
}