JEditorPane如何显示本地的HTML文件,希望给个例子,谢谢!!

解决方案 »

  1.   

    给楼主给例子,这个肯定可以用import java.awt.Dimension;
    import java.awt.Rectangle;
    import java.awt.Toolkit;
    import java.io.File;
    import java.io.IOException;
    import java.net.MalformedURLException;import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JEditorPane;public class IndexPanel extends JPanel
    { private static final long serialVersionUID = 1L; private JLabel jLabel24 = null; private JEditorPane editorPane = null; private JScrollPane scrollPane = null; /**
     * This is the default constructor
     */
    public IndexPanel()
    {
    super();
    initialize();
    } /**
     * This method initializes this
     * 
     * @return void
     */
    private void initialize()
    { this.setLayout(null);
    this.setSize(new Dimension(610, 660)); this.add(getJLabel24(), null);
    this.add(getScrollPane(), null);
    } /**
     * This method initializes indexPanel
     * 
     * @return javax.swing.JPanel
     */
    private JLabel getJLabel24()
    {
    if (jLabel24 == null)
    {
    jLabel24 = new JLabel();
    jLabel24.setText("目录和索引");
    jLabel24.setBounds(new Rectangle(207, 16, 202, 18));
    }
    return jLabel24;
    } /**
     * This method initializes editorPane
     * 
     * @return javax.swing.JEditorPane
     */
    private JEditorPane getEditorPane()
    {
    if (editorPane == null)
    {
    editorPane = new JEditorPane();
    editorPane.setEditable(false);
    editorPane.setContentType("text/html");
    File file = new File(
    ".\\resources\\光盘使用说明.htm"); String strURL = file.getAbsolutePath();
    strURL = "file:" + strURL; try
    {
    editorPane.setPage(strURL);
    }
    catch (MalformedURLException e)
    {
    // TODO Auto-generated catch block
    System.err.println(e.toString());
    System.exit(1);
    }
    catch (IOException e)
    {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    return editorPane;
    } /**
     * This method initializes scrollPane
     * 
     * @return javax.swing.JScrollPane
     */
    private JScrollPane getScrollPane()
    {
    if (scrollPane == null)
    {
    scrollPane = new JScrollPane();
    scrollPane.setBounds(new Rectangle(4, 47, 622, 659));
    scrollPane.setViewportView(getEditorPane());
    }
    return scrollPane;
    }}