Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/90.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何打开html文件?_Java_Html_Swing_Netbeans_Embedded Resource - Fatal编程技术网

Java 如何打开html文件?

Java 如何打开html文件?,java,html,swing,netbeans,embedded-resource,Java,Html,Swing,Netbeans,Embedded Resource,我一直试图在单击“帮助”按钮时打开一个htmlfile。我需要将文件导入到源程序包中,这样在构建之后就不会显示错误。我正在使用netbeans。当我将文件复制到src并尝试运行该文件时,它显示编译错误。我想知道如何将文件添加到src,以及如何通过询问用户系统中安装的浏览器列表来打开文件。这是我搜索和尝试过的代码..提前谢谢 try { File htmlFile = new File(this.getClass().getResource("help.

我一直试图在单击“帮助”按钮时打开一个
htmlfile
。我需要将文件导入到源程序包中,这样在构建之后就不会显示错误。我正在使用
netbeans
。当我将文件复制到
src
并尝试运行该文件时,它显示编译错误。我想知道如何将文件添加到
src
,以及如何通过询问用户系统中安装的浏览器列表来打开文件。这是我搜索和尝试过的代码..提前谢谢

  try 
  {           
      File htmlFile = new File(this.getClass().getResource("help.html").getFile());
      Desktop.getDesktop().browse(htmlFile.toURI());
  } 
  catch (IOException ex)
  {
      System.out.println(ex);
  }

我尝试使用我的桌面
html
文件。现在它可以正常工作了。 它将对您有用。(我的默认浏览器是
chrome

试试看

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Desktop;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class FileOpenBrowser {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        JFrame frame = new JFrame("FileOpenBrowser"); // set API Name
        JPanel topPanel = new JPanel(new FlowLayout()); // set Panel Layout

        File urlDesktop = new File(
                "C://Documents and Settings/Hariharan/Desktop/help.html");

        Button btn = new Button("Help");
        btn.setBounds(50, 100, 60, 30);
        topPanel.add(btn);

        frame.add(topPanel, BorderLayout.PAGE_START);

        frame.setSize(300, 300);
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        btn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    Desktop.getDesktop().open(urlDesktop);
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        });
    }
}
试试这个


您必须给我们一个例外情况。@KarthikeyanVaithiling很抱歉我没有运行它,因为它显示代码编译时有错误。如果我将文件添加到
src
包中,则代码未成功编译您必须给出的编译错误us@KarthikeyanVaithilingam是 啊对于您的评论,它尝试编译并运行代码,但出现了错误。那么为什么
ide
显示
一个或多个编译错误的项目???它会影响生成后吗?但是生成后它肯定会显示错误,因为您提到了文件的路径。。但是我需要将
.html文件
添加到
src
文件夹本身,然后调用它
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Desktop;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class FileOpenBrowser {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        JFrame frame = new JFrame("FileOpenBrowser"); // set API Name
        JPanel topPanel = new JPanel(new FlowLayout()); // set Panel Layout

        Button btn = new Button("Help");
        btn.setBounds(50, 100, 60, 30);
        topPanel.add(btn);

        frame.add(topPanel, BorderLayout.PAGE_START);

        frame.setSize(300, 300);
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        btn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    System.out.println("Start..");
                    File file = new java.io.File("src/help.html").getAbsoluteFile();
                    Desktop.getDesktop().open(file);                    
                    System.out.println("End..");
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        });
    }
}