简体   繁体   English

在Java Swing应用程序中启动URL

[英]Launching a URL in a Java Swing application

How do I launch a URL in the user's default browser, in code from a Java Swing application? 如何在Java Swing应用程序的代码中在用户的默认浏览器中启动URL?

There is this Netbeans library, but the jar dont seem to contain the classes mentioned in the example. 这个 Netbeans库,但jar似乎不包含示例中提到的类。

And there seems to be a number of old bespoke examples around. 并且似乎有许多旧的定制示例

But are there any killer solutions? 但有没有任何杀手解决方案?

If you're running on JDK 1.6, you java.awt.Desktop . 如果您在JDK 1.6上运行,则使用java.awt.Desktop

Desktop.getDesktop().browse(new java.net.URI("www.google.com")); Desktop.getDesktop()。browse(new java.net.URI(“www.google.com”));

If running on an earlier JDK, I believe that you can download the JDIC library . 如果在早期的JDK上运行,我相信你可以下载JDIC库 Or hack something together by spawning processes. 或者通过产生过程来破解某些东西。

To expand upon kdgregory's answer , the The Java Desktop API , available from Java 6, provides integration with the desktop with functionality such as launching default web browsers and mail clients. 为了扩展kdgregory的答案 ,Java 6提供的Java Desktop API提供了与桌面的集成,其功能包括启动默认Web浏览器和邮件客户端。

Launching a web browser can be achieved by using the Desktop.browse method. 使用Desktop.browse方法可以启动Web浏览器。

For example, launching http://stackoverflow.com can be acheived by the following: 例如,启动http://stackoverflow.com可以通过以下方式实现:

Desktop.getDesktop().browse(new URI("http://stackoverflow.com"));

More information: 更多信息:

您可以查看BrowserLauncher ,尽管最新版本的JDK正在尝试使其过时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM