让java gui在web浏览器中打开一个网页 [英] Getting java gui to open a webpage in web browser

查看:408
本文介绍了让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或更高版本,请参阅< a href =http://java.sun.com/javase/6/docs/api/java/awt/Desktop.html =noreferrer> 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天全站免登陆