简体   繁体   English

如何将HTML文件显示为网页?

[英]How can I display an HTML file as a web page?

My code reads an HTML file and I want to show it in new frame as a web page. 我的代码读取一个HTML文件,我想在新框架中将其显示为网页。 But I don't know - how can I do this? 但我不知道 - 我怎么能这样做?

This is my code: 这是我的代码:

public class EditorPaneLoad extends JFrame{ 

public EditorPaneLoad() throws Exception{

    FileReader reader = new FileReader("a.html");
    JEditorPane editor = new JEditorPane();
    JTextPane editor = new JTextPane();
    editor.setContentType( "text/html" );
    editor.setEditable( false );
    editor.read(reader, null);
    //System.out.println(editor.getText());
    //System.out.println("\n------------\n");
    Document doc = editor.getDocument();
    // System.out.println(doc.getText(0, doc.getLength()));
    JScrollPane scrollPane = new JScrollPane( editor );
    scrollPane.setPreferredSize( new Dimension(300, 200) );
    getContentPane().add( scrollPane );
}

public static void main(String[] args)
    throws Exception
{
    EditorPaneLoad frame = new EditorPaneLoad();
    frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
    frame.pack();
    frame.setLocationRelativeTo( null );
    frame.setVisible(true);
}
}

If I get it right, you want to render HTML through a desktop window of your application. 如果我做对了,你想通过应用程序的桌面窗口呈现HTML。

Perhaps flying saucer would help you. 也许飞碟会帮助你。 An alternative, Lobo would be rendered using javafx but it would only support HTML 4. 另一种方法是, Lobo将使用javafx呈现,但它只支持HTML 4。

Hope I helped! 希望我帮忙!

// opens "a.html" in the default browser..
Desktop.getDesktop().open(new File("a.html"));

See Desktop.open(File) for details. 有关详细信息,请参见Desktop.open(File)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM