web-dev-qa-db-ja.com

ブラウザでJavaボタンを使用してリンクを開きますか?

デフォルトのブラウザでボタンをクリックして、次の行に沿ってリンクを開くにはどうすればよいですか

button.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        open("www.google.com"); // just what is the 'open' method?
    }
});

97
Malasuerte94

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;
}
216
FThompson
public static void openWebpage(String urlString) {
    try {
        Desktop.getDesktop().browse(new URL(urlString).toURI());
    } catch (Exception e) {
        e.printStackTrace();
    }
}
34
Daniel
try {
    Desktop.getDesktop().browse(new URL("http://www.google.com").toURI());
} catch (Exception e) {}

注:Java.netから必要なインポートを含める必要があります

24
AdelM

デスクトップ環境を使用しないソリューションは、 BrowserLauncher2 です。このソリューションはLinuxでより一般的であり、デスクトップは常に利用できるとは限りません。

長さの答えは https://stackoverflow.com/a/21676290/873282 に投稿されています

5
koppor
private void ButtonOpenWebActionPerformed(Java.awt.event.ActionEvent evt) {                                         
        try { 
         String url = "https://www.google.com";
         Java.awt.Desktop.getDesktop().browse(Java.net.URI.create(url));
       }
       catch (Java.io.IOException e) {
           System.out.println(e.getMessage());
       }
    }    
3
user8039515

これは古い質問であることは知っていますが、Ubuntu 18.04のようにDesktop.getDesktop()が予期しないクラッシュを引き起こすことがあります。したがって、次のようにコードを書き直す必要があります。

public static void openURL(String domain)
{
    String url = "https://" + domain;
    Runtime rt = Runtime.getRuntime();
    try {
        if (MUtils.isWindows()) {
            rt.exec("rundll32 url.dll,FileProtocolHandler " + url).waitFor();
            Debug.log("Browser: " + url);
        } else if (MUtils.isMac()) {
            String[] cmd = {"open", url};
            rt.exec(cmd).waitFor();
            Debug.log("Browser: " + url);
        } else if (MUtils.isUnix()) {
            String[] cmd = {"xdg-open", url};
            rt.exec(cmd).waitFor();
            Debug.log("Browser: " + url);
        } else {
            try {
                throw new IllegalStateException();
            } catch (IllegalStateException e1) {
                MUtils.alertMessage(Lang.get("desktop.not.supported"), MainPn.getMainPn());
                e1.printStackTrace();
            }
        }
    } catch (IOException | InterruptedException e) {
        e.printStackTrace();
    }
}

public static boolean isWindows()
{
    return OS.contains("win");
}

public static boolean isMac()
{
    return OS.contains("mac");
}

public static boolean isUnix()
{
    return OS.contains("nix") || OS.contains("nux") || OS.indexOf("aix") > 0;
}

次に、インスタンスからこのヘルパーを呼び出すことができます。

button.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        MUtils.openURL("www.google.com"); // just what is the 'open' method?
    }
});
0
Teocci
public static void openWebPage(String url) {
        try {
            Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
            if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
                desktop.browse(new URI(url));
            }
            throw new NullPointerException();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, url, "", JOptionPane.PLAIN_MESSAGE);
        }
    }
0
sambuca