1

我有一个在浏览器中打开 URL 的按钮:

URI uri = new URI("http://google.com/");
Desktop dt = Desktop.getDesktop();
dt.browse(uri.toURL()); // has error

但我在最后一条语句中收到以下错误:

The method browse(URI) in the type Desktop is not applicable for the arguments (URL)

感谢您的任何建议。

4

3 回答 3

3

找到解决方案:
1. 删除.toURL()
2. 使用try catch

try
{
    URI uri = new URI("http://google.com/");
    Desktop dt = Desktop.getDesktop();
    dt.browse(uri);
}
catch(Exception ex){}
于 2013-10-15T07:31:16.497 回答
2

它告诉您的是,您正在发送一个 URL 对象,而它需要一个 URI。

只是改变

dt.browse(uri.toURL()); // has error

dt.browse(uri); // has error

在能够使用 Desktop 之前,您必须考虑它是否受支持

if (Desktop.isDesktopSupported()) {
        desktop = Desktop.getDesktop();
        // now enable buttons for actions that are supported.
        enableSupportedActions();
}

和 enableSupportedActions

private void enableSupportedActions() {
    if (desktop.isSupported(Desktop.Action.BROWSE)) {
        txtBrowserURI.setEnabled(true);
        btnLaunchBrowser.setEnabled(true);
    }
}

这表明您还必须检查是否还支持 BROWSE 操作。

于 2013-10-15T07:06:00.770 回答
1

使用这样的东西

try {Desktop.getDesktop().browse(new URI("http://www.google.com"));
} catch (Exception e) 
{JOptionPane.showMessageDialog(null,e);}
} 
于 2014-03-26T13:12:17.277 回答