获取java gui在Web浏览器中打开网页

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/602032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 16:43:05  来源:igfitidea点击:

Getting java gui to open a webpage in web browser

javahtmluser-interface

提问by The Special One

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 gui 来打开网页。因此,gui 运行一些执行操作的代码,然后生成一个 html 文件。然后我希望该文件在创建后立即在 Web 浏览器(最好是 Firefox)中打开。我该怎么做?

回答by Dan Vinton

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

如果您使用的是 Java 6 或更高版本,请参阅桌面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);

回答by Brian Agnew

I've used BrowserLauncher2with success. It'll invoke the default browser on all platforms tested. I use this for demoing software via JNLP. The software downloads, runs and drives the user's browser to information pages/feedback etc.

我已经成功地使用了BrowserLauncher2。它将调用所有测试平台上的默认浏览器。我用它来通过 JNLP 演示软件。该软件下载、运行并驱动用户浏览器到信息页面/反馈等。

JDK 1.4 and above, I believe.

我相信 JDK 1.4 及更高版本。

回答by Ankur

Ya, But if you want to open the webpage in your default web browser by a java program then you can try using this code.

是的,但是如果您想通过 Java 程序在默认 Web 浏览器中打开网页,那么您可以尝试使用此代码。

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

回答by Troyseph

I know that all of these answers have basically answered the question, but here is a the code for a method that fails gracefully.

我知道所有这些答案基本上都回答了这个问题,但这里有一个优雅失败的方法的代码。

Note that the string can be the location of an html file

注意字符串可以是一个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);
        }
}