How can I open a link in default browser with a button click, along the lines of
如何在按钮单击的情况下打开默认浏览器中的链接
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
open("www.google.com"); // just what is the 'open' method?
}
});
?
?
6 个解决方案
#1
207
Use the Desktop#browse(URI) method. It opens a URI in the user's default browser.
使用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;
}
#2
30
public static void openWebpage(String urlString) {
try {
Desktop.getDesktop().browse(new URL(urlString).toURI());
} catch (Exception e) {
e.printStackTrace();
}
}
#3
22
try {
Desktop.getDesktop().browse(new URL("http://www.google.com").toURI());
} catch (Exception e) {}
note: you have to include necessary imports from java.net
注意:您必须从java.net中包含必要的导入
#4
5
A solution without the Desktop environment is BrowserLauncher2. This solution is more general as on Linux, Desktop is not always available.
没有桌面环境的解决方案是BrowserLauncher2。这个解决方案比Linux更通用,桌面并不总是可用。
The lenghty answer is posted at https://*.com/a/21676290/873282
长篇答案发布在https://*.com/a/21676290/873282
#5
2
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());
}
}
#6
0
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);
}
}
#1
207
Use the Desktop#browse(URI) method. It opens a URI in the user's default browser.
使用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;
}
#2
30
public static void openWebpage(String urlString) {
try {
Desktop.getDesktop().browse(new URL(urlString).toURI());
} catch (Exception e) {
e.printStackTrace();
}
}
#3
22
try {
Desktop.getDesktop().browse(new URL("http://www.google.com").toURI());
} catch (Exception e) {}
note: you have to include necessary imports from java.net
注意:您必须从java.net中包含必要的导入
#4
5
A solution without the Desktop environment is BrowserLauncher2. This solution is more general as on Linux, Desktop is not always available.
没有桌面环境的解决方案是BrowserLauncher2。这个解决方案比Linux更通用,桌面并不总是可用。
The lenghty answer is posted at https://*.com/a/21676290/873282
长篇答案发布在https://*.com/a/21676290/873282
#5
2
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());
}
}
#6
0
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);
}
}