3 回答
TA贡献1780条经验 获得超4个赞
使用Desktop#browse(URI)方法。它将在用户的默认浏览器中打开一个URI。
public static boolean openWebpage(URI uri) {
Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
try {
desktop.browse(uri);
return true;
} catch (Exception e) {
e.printStackTrace();
}
}
return false;
}
public static boolean openWebpage(URL url) {
try {
return openWebpage(url.toURI());
} catch (URISyntaxException e) {
e.printStackTrace();
}
return false;
}
TA贡献1815条经验 获得超6个赞
public static void openWebpage(String urlString) {
try {
Desktop.getDesktop().browse(new URL(urlString).toURI());
} catch (Exception e) {
e.printStackTrace();
}
}
添加回答
举报