如何通过单击按钮在默认浏览器中打开链接,如下所示
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
open("www.google.com"); // just what is the 'open' method?
}
});
发布于 2012-06-10 16:55:29
使用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;
}
发布于 2013-06-02 10:32:06
public static void openWebpage(String urlString) {
try {
Desktop.getDesktop().browse(new URL(urlString).toURI());
} catch (Exception e) {
e.printStackTrace();
}
}
发布于 2013-08-20 01:37:52
try {
Desktop.getDesktop().browse(new URL("http://www.google.com").toURI());
} catch (Exception e) {}
注意:您必须包含从java.net
导入的必要内容
https://stackoverflow.com/questions/10967451
复制相似问题