我如何在默认浏览器中单击按钮以打开以下链接:
button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { open("www.google.com”); // just what is the ‘open’ method? } }); ?
使用Desktop#browse(URI)方法。它将在用户的默认浏览器中打开一个URI。
Desktop#browse(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; }