如何在按钮点击时使用默认浏览器打开链接,类似于:
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
open("www.google.com"); // just what is the 'open' method?
}
});
?
如何在按钮点击时使用默认浏览器打开链接,类似于:
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
open("www.google.com"); // just what is the 'open' method?
}
});
?
使用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;
}
new ProcessBuilder("x-www-browser", uri.toString());
来“解决”这个问题。如果存在安全限制,你会认为ProcessBuilder调用不起作用。但它确实有效。我不知道为什么desktop.browse(uri)
不起作用,但我已经看到很多人都遇到了这个问题。我猜可能是Netbeans的问题,但我不确定。 - MountainXopenWebpage
的替代方法可以使用Runtime.exec(..)
并迭代通过预定义的流行浏览器名称集合,将它们传递给URL。然而,这也有一个缺点,即不能为使用不常见浏览器的用户运行,但是当我有空闲时间时,我会编写并将其添加到此答案中。 - FThompsonpublic static void openWebpage(String urlString) {
try {
Desktop.getDesktop().browse(new URL(urlString).toURI());
} catch (Exception e) {
e.printStackTrace();
}
}
try {
Desktop.getDesktop().browse(new URL("http://www.google.com").toURI());
} catch (Exception e) {}
注意:你需要从java.net
中导入必要的引用。
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());
}
}
我知道这是一个老问题,但有时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?
}
});
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);
}
}