如何在Java中通过桌面对象打开URL?
我有一个在浏览器中打开URL的按钮:如何在Java中通过桌面对象打开URL?,java,url,desktop,Java,Url,Desktop,我有一个在浏览器中打开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) 感谢您的建议。它告诉您,您正
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)
感谢您的建议。它告诉您,您正在发送一个URL对象,而它需要一个URI 换衣服
dt.browse(uri.toURL()); // has error
到
在使用桌面之前,必须考虑是否支持
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);
}
}
这表明您还必须检查是否也支持浏览操作。它告诉您的是,您正在发送一个URL对象,而它需要一个URI 换衣服
dt.browse(uri.toURL()); // has error
到
在使用桌面之前,必须考虑是否支持
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);
}
}
这表明您还必须检查是否也支持浏览操作。找到了解决方案:1.移除.toURL()
2.使用尝试捕捉块
try
{
URI uri = new URI("http://google.com/");
Desktop dt = Desktop.getDesktop();
dt.browse(uri);
}
catch(Exception ex){}
找到的解决方案:1.移除.toURL()
2.使用尝试捕捉块
try
{
URI uri = new URI("http://google.com/");
Desktop dt = Desktop.getDesktop();
dt.browse(uri);
}
catch(Exception ex){}
用这样的东西
try {Desktop.getDesktop().browse(new URI("http://www.google.com"));
} catch (Exception e)
{JOptionPane.showMessageDialog(null,e);}
}
用这样的东西
try {Desktop.getDesktop().browse(new URI("http://www.google.com"));
} catch (Exception e)
{JOptionPane.showMessageDialog(null,e);}
}
一定要避免像那样的空挡块。这是导致难以跟踪的无声bug的原因。一定要避免像这样的空捕获块。这是导致难以跟踪的无声bug的原因。