打开浏览器与Java按钮链接? [英] Open a link in browser with java button?
本文介绍了打开浏览器与Java按钮链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我怎样可以打开一个按钮,点击默认浏览器的链接,沿行
How can I open a link in default browser with a button click, along the lines of
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
open("www.google.com"); // just what is the 'open' method?
}
});
推荐答案
使用的<一个href=\"http://docs.oracle.com/javase/7/docs/api/java/awt/Desktop.html#browse%28java.net.URI%29\">Desktop#browse(URI)方法。它会打开一个URI在用户的默认浏览器。
Use the Desktop#browse(URI) method. It opens a URI in the user's default browser.
public static void openWebpage(URI uri) {
Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
try {
desktop.browse(uri);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void openWebpage(URL url) {
try {
openWebpage(url.toURI());
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
这篇关于打开浏览器与Java按钮链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文