我想要一个Java GUI来打开网页。因此,gui运行一些代码来执行某些操作,然后生成一个html文件。 然后,我希望此文件在创建后立即在Web浏览器(最好是Firefox)中打开。 我该怎么做呢?获取java gui在Web浏览器中打开网页
34
A
回答
39
如果您使用的是Java 6或更高版本,请参阅Desktop API,特别是browse。像这样使用它(未测试):
// using this in real life, you'd probably want to check that the desktop
// methods are supported using isDesktopSupported()...
String htmlFilePath = "path/to/html/file.html"; // path to your new file
File htmlFile = new File(htmlFilePath);
// open the default web browser for the HTML page
Desktop.getDesktop().browse(htmlFile.toURI());
// if a web browser is the default HTML handler, this might work too
Desktop.getDesktop().open(htmlFile);
0
我用BrowserLauncher2成功。它会调用所有测试平台上的默认浏览器。我使用它通过JNLP演示软件。该软件下载,运行和驱动用户的浏览器信息页面/反馈等。
JDK 1.4及以上,我相信。
26
雅,但是如果你想通过java程序打开你的默认网页浏览器的网页,那么你可以尝试使用这段代码。
/// file OpenPageInDefaultBrowser.java
public class OpenPageInDefaultBrowser {
public static void main(String[] args) {
try {
//Set your page url in this string. For eg, I m using URL for Google Search engine
String url = "http://www.google.com";
java.awt.Desktop.getDesktop().browse(java.net.URI.create(url));
}
catch (java.io.IOException e) {
System.out.println(e.getMessage());
}
}
}
/// End of file
2
我知道所有这些答案已经基本回答了这个问题,但这里是一个优雅地失败的方法的代码。
注意,字符串可以是HTML文件
/**
* If possible this method opens the default browser to the specified web page.
* If not it notifies the user of webpage's url so that they may access it
* manually.
*
* @param url
* - this can be in the form of a web address (http://www.mywebsite.com)
* or a path to an html file or SVG image file e.t.c
*/
public static void openInBrowser(String url)
{
try
{
URI uri = new URL(url).toURI();
Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
desktop.browse(uri);
} else {
throw new Exception("Desktop not supported, cannout open browser automatically");
}
}
catch (Exception e)
{
/*
* I know this is bad practice
* but we don't want to do anything clever for a specific error
*/
e.printStackTrace();
// Copy URL to the clipboard so the user can paste it into their browser
StringSelection stringSelection = new StringSelection(url);
Clipboard clpbrd = Toolkit.getDefaultToolkit().getSystemClipboard();
clpbrd.setContents(stringSelection, null);
// Notify the user of the failure
WindowTools.informationWindow("This program just tried to open a webpage." + "\n"
+ "The URL has been copied to your clipboard, simply paste into your browser to access.",
"Webpage: " + url);
}
}
相关问题
- 1. 在vb“网页浏览器控件”中打开Chrome浏览器
- 2. WebWorks:打开网页浏览器?
- 3. web浏览器的I帧在默认浏览器中打开
- 4. 打开网络浏览器
- 5. 作为网页浏览器的幻象在java中打开网站代码
- 6. 如何在Android中强制打开新浏览器(从网页)?
- 7. 获取网页浏览器cookies登录
- 8. 在web浏览器托管的网页
- 9. 在外部浏览器中打开web浏览器控件中的链接?
- 10. 使用闪光灯在浏览器中打开一个网页
- 11. Python - 烧瓶 - 在默认浏览器中打开两个网页
- 12. Ionic无法在系统浏览器中打开网页
- 13. LinkLabel ...在默认网页浏览器中打开?
- 14. 检查在浏览器中打开的网页地址
- 15. 打开Web浏览器点击默认浏览器
- 16. 如何打开Eclipse Web浏览器?
- 17. 打开默认Web浏览器
- 18. 在后台打开浏览器 - 在后台加载网页
- 19. Web浏览器开发
- 20. Windows通用应用程序打开网页浏览器链接
- 21. 浏览器无法打开php页面?
- 22. 打开浏览器
- 23. 启用浏览器的InfoPath表单不能在Web浏览器中打开
- 24. 打开浏览器到网页安卓应用
- 25. 儿童浏览器不打开网页在ios phonegap 1.3
- 26. 在启动应用程序时打开网页浏览器
- 27. Python网络浏览器不能打开浏览器
- 28. 在网页中浏览/浏览按钮
- 29. 从winform打开网络浏览器
- 30. 在默认浏览器中检测打开的网址
嘿的位置。仍然无法让它工作。我收到以下错误:“错误消息:系统找不到指定的路径。”这是即使我已经给出了确切的路径。 – 2009-03-03 12:26:29
@TheSpecialOne - 你提供一个文件的_local_路径吗?例如:在一个窗口框中,htmlFilePath看起来像“c:\ path \ to \ file.html”。不要使用URL语法... – 2009-03-04 11:34:17