Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/82.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_File_Jeditorpane - Fatal编程技术网

Java 如何在窗格中显示html文件

Java 如何在窗格中显示html文件,java,html,swing,file,jeditorpane,Java,Html,Swing,File,Jeditorpane,我试图在一个窗格中显示一个html文件 该文件保存在项目文件夹中,并由程序生成。这是一张名为FredReceipt.html的收据 我知道如何使用jeditorpane作为url,但我不知道如何通过教程等加载文件。。。我一直在网上阅读。我想使用相对url加载文件。这就是我目前所拥有的,它不起作用(显然),它正在捕获IOException public void showReceipt() { receiptPanel = new JPanel(); receiptPanel.se

我试图在一个窗格中显示一个html文件

该文件保存在项目文件夹中,并由程序生成。这是一张名为FredReceipt.html的收据

我知道如何使用jeditorpane作为url,但我不知道如何通过教程等加载文件。。。我一直在网上阅读。我想使用相对url加载文件。这就是我目前所拥有的,它不起作用(显然),它正在捕获IOException

public void showReceipt() {
    receiptPanel = new JPanel();
    receiptPanel.setVisible(true);
    receiptPanel.setBackground(new Color(250,251,253)); 
    String url = "FredReceipt.html";
    try {
      JEditorPane htmlPane = new JEditorPane("FredReceipt.html");
      htmlPane.setEditable(false);
      receiptPanel.add(new JScrollPane(htmlPane));
    } catch(IOException ioe) {
      System.err.println("Error displaying " + url);
    }

}
我还尝试过使用“setPage()”方法,如下所示:

public void showReceipt() {
    receiptPanel = new JPanel();
    receiptPanel.setVisible(true);
    receiptPanel.setBackground(new Color(250,251,253)); 
    try {
      JEditorPane htmlPane = new JEditorPane();
      htmlPane.setPage(new URL("FredReceipt.html"));
      htmlPane.setEditable(false);
      receiptPanel.add(new JScrollPane(htmlPane));
    } catch(IOException ioe) {
      System.err.println("Error displaying file");
    }

}
File tempDir = new File(System.getProperty("java.io.tempdir"));
File receiptFile = File.createTempFile("FredReceipt", "html", tempDir);
receiptFile.deleteOnExit();
// fill the file with mark-up
// ...
// end filling
editorPane.setPage(receiptFile.toURI().toURL());
html显然不是一个url,但我读到文件可以像url一样读取,我只是没有找到正确的方法


我希望我的问题不要太傻,谢谢

文件不应写入安装应用程序的同一目录。由于该数据是由应用程序生成的,因此它似乎是临时的。在这种情况下,最好将
java.io.tmpdir
作为一个输入,并请求。大概是这样的:

public void showReceipt() {
    receiptPanel = new JPanel();
    receiptPanel.setVisible(true);
    receiptPanel.setBackground(new Color(250,251,253)); 
    try {
      JEditorPane htmlPane = new JEditorPane();
      htmlPane.setPage(new URL("FredReceipt.html"));
      htmlPane.setEditable(false);
      receiptPanel.add(new JScrollPane(htmlPane));
    } catch(IOException ioe) {
      System.err.println("Error displaying file");
    }

}
File tempDir = new File(System.getProperty("java.io.tempdir"));
File receiptFile = File.createTempFile("FredReceipt", "html", tempDir);
receiptFile.deleteOnExit();
// fill the file with mark-up
// ...
// end filling
editorPane.setPage(receiptFile.toURI().toURL());

文件不应写入安装应用程序的同一目录。由于该数据是由应用程序生成的,因此它似乎是临时的。在这种情况下,最好将
java.io.tmpdir
作为一个输入,并请求。大概是这样的:

public void showReceipt() {
    receiptPanel = new JPanel();
    receiptPanel.setVisible(true);
    receiptPanel.setBackground(new Color(250,251,253)); 
    try {
      JEditorPane htmlPane = new JEditorPane();
      htmlPane.setPage(new URL("FredReceipt.html"));
      htmlPane.setEditable(false);
      receiptPanel.add(new JScrollPane(htmlPane));
    } catch(IOException ioe) {
      System.err.println("Error displaying file");
    }

}
File tempDir = new File(System.getProperty("java.io.tempdir"));
File receiptFile = File.createTempFile("FredReceipt", "html", tempDir);
receiptFile.deleteOnExit();
// fill the file with mark-up
// ...
// end filling
editorPane.setPage(receiptFile.toURI().toURL());

JEditorPane是一种奇特的文本区域,可以显示来自不同文件格式的文本

以下链接可能对您有所帮助。

JEditorPane是一种奇特的文本区域,可以显示来自不同文件格式的文本

以下链接可能对您有所帮助。

你说的“它不工作”是什么意思?具体点。对不起,它抓住了这个问题。我在失去希望并问了这个问题之后,设法找到了解决办法。对不起,浪费了大家的时间。当我被允许的时候,我会发布它(我不能发布几个小时,因为我有不到100个代表)。你说的“它不工作”是什么意思?具体点。对不起,它抓住了这个问题。我在失去希望并问了这个问题之后,设法找到了解决办法。对不起,浪费了大家的时间。当我被允许的时候,我会发布它(我不能发布几个小时,因为有不到100个代表)。