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

时间:2014-01-13 20:22:18

标签: java html swing web jeditorpane

我的代码读取HTML文件,我希望将其作为网页显示在新框架中。 但我不知道 - 我怎么能这样做?

这是我的代码:

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

2 个答案:

答案 0 :(得分:2)

如果我做对了,你想通过应用程序的桌面窗口呈现HTML。

也许flying saucer可以帮到你。另一种选择,Lobo将使用javafx呈现,但它只支持HTML 4。

希望我帮忙!

答案 1 :(得分:1)

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

有关详细信息,请参阅Desktop.open(File)