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

时光怂恿深爱的人放手 提交于 2019-12-11 12:09:12

问题


My code reads an HTML file and I want to show it in new frame as a web page. 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);
}
}

回答1:


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

See Desktop.open(File) for details.




回答2:


If I get it right, you want to render HTML through a desktop window of your application.

Perhaps flying saucer would help you. An alternative, Lobo would be rendered using javafx but it would only support HTML 4.

Hope I helped!



来源:https://stackoverflow.com/questions/21100635/how-can-i-display-an-html-file-as-a-web-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!