猿问

使用Java按钮在浏览器中打开链接?

我如何在默认浏览器中单击按钮以打开以下链接:


button.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent e) {

        open("www.google.com"); // just what is the 'open' method?

    }

});


FFIVE
浏览 1045回答 3
3回答

Helenr

使用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;}

红糖糍粑

public static void openWebpage(String urlString) {    try {        Desktop.getDesktop().browse(new URL(urlString).toURI());    } catch (Exception e) {        e.printStackTrace();    }}
随时随地看视频慕课网APP

相关分类

Java
我要回答