内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用
如何通过单击按钮打开默认浏览器中的链接,如
button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { open("www.google.com"); // just what is the 'open' method? } });
?
在用户的默认浏览器中打开一个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; }