获取java gui在Web浏览器中打开网页 [英] Getting java gui to open a webpage in web browser

查看:34
本文介绍了获取java gui在Web浏览器中打开网页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 java gui 来打开网页.因此,gui 运行一些执行操作的代码,然后生成一个 html 文件.然后我希望这个文件在创建后立即在 Web 浏览器(最好是 Firefox)中打开.我该怎么做?

I am trying to get a java gui to open a web page. So the gui runs some code that does things and then produces a html file. I then want this file to open in a web browser (preferrably Firefox) as soon as it is created. How would I go about doing that?

推荐答案

如果您使用的是 Java 6 或更高版本,请参阅 Desktop API,特别是 浏览.像这样使用它(未经测试):

If you're using Java 6 or above, see the Desktop API, in particular browse. Use it like this (not tested):

// 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);

这篇关于获取java gui在Web浏览器中打开网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆