是否使用java按钮在浏览器中打开链接?

是否使用java按钮在浏览器中打开链接?,java,swing,browser,hyperlink,desktop,Java,Swing,Browser,Hyperlink,Desktop,如何在默认浏览器中单击按钮打开链接 button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { open("www.google.com"); // just what is the 'open' method? } }); ?使用该方法。它在用户的默认浏览器中打开一个URI public static boolean openWebpa

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

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

注意:您必须包含从
java.net

导入的必要内容,不使用桌面环境的解决方案是无效的。这个解决方案更通用,因为在Linux上,桌面并不总是可用的

详细答案发布在

private void ButtonOpenWebActionPerformed(java.awt.event.ActionEvent evt){
试一试{
字符串url=”https://www.google.com";
java.awt.Desktop.getDesktop().browse(java.net.URI.create(url));
}捕获(java.io.ioe异常){
System.out.println(e.getMessage());
}
}
公共静态无效openWebPage(字符串url){
试一试{
Desktop Desktop=Desktop.isDesktopSupported()?Desktop.getDesktop():null;
if(desktop!=null&&desktop.isSupported(desktop.Action.BROWSE)){
浏览(新URI(url));
}
抛出新的NullPointerException();
}捕获(例外e){
showMessageDialog(null,url,“,JOptionPane.PLAIN_消息);
}
}

我知道这是一个老问题,但有时
Desktop.getDesktop()
会产生意外的崩溃,就像Ubuntu 18.04一样。因此,我必须像这样重新编写代码:

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?
    }
});

您可能已经尝试过查看您的答案中使用的!那些文件。非常方便,可以将它们附加到IDE中,也可以将在线版本添加到书签中。重复问题:这在Netbeans 7.x创建的JAR文件中似乎不起作用。它在代码从Netbeans运行时工作,但在部署为JAR文件时不工作。。。至少以我的经验来看。我仍在寻找解决方案。@MountainX调试并验证桌面是否受支持,以及安全实现是否限制您访问桌面实例。如果您将JAR作为小程序运行,那么安全性可能是罪魁祸首。@Vulcan--我没有将JAR作为小程序运行。我不知道有任何安全设置会阻止它工作。我通过调用
newprocessbuilder(“x-www-browser”,uri.toString())来“解决”它。您可能会认为,如果存在安全限制,ProcessBuilder调用将不起作用。但它确实有效。我不知道为什么
desktop.browse(uri)
不起作用,但我发现它对很多人都不起作用。我猜这可能是Netbeans的问题,但我不知道。如果用户为文件扩展名(如“html”)指定了自定义的“打开方式”操作,则这不会打开浏览器,但用户链接的程序。。。。这根本不是解决办法@圣大点;另一种选择是
openWebpage
可以使用
Runtime.exec(..)
并迭代一组预定义的流行浏览器名称,将URL传递给它们。不过,这也有一个警告,就是不要为那些浏览器不太清晰的用户运行,但我会在有空的时候尽快编写并添加到这个答案中。它工作得非常好!谢谢
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?
    }
});