Getting java gui to open a webpage in web browser

后端 未结 4 1570
野的像风
野的像风 2020-11-28 10:20

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 (p

4条回答
  •  忘掉有多难
    2020-11-28 11:05

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

提交回复
热议问题