【问题标题】:Browser won't show HTML File浏览器不会显示 HTML 文件
【发布时间】:2012-03-08 00:02:26
【问题描述】:

我有这两个类,它们将成为一个更大项目的一部分,但是由于某些奇怪的原因,当我运行 main() 时,浏览器不会显示 HTML 文件。

这是两个类:

主要:

import java.awt.BorderLayout;
import java.awt.Component;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class main {

    public static void main(String[] args) {
        BrowserFrame browser = new BrowserFrame();

        JFrame mainFrame = new JFrame();
        Thread browserThread = new Thread();

        mainFrame.getContentPane().add(browser);
        mainFrame.setSize(550,550);
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        browserThread.start();
        browser.setVisible(true);
        mainFrame.setVisible(true);
    }
}

浏览器框架

import java.awt.BorderLayout;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.JEditorPane;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.StyleSheet;

public class BrowserFrame extends javax.swing.JPanel {

    public void BrowserFrame()  {

        SwingUtilities.invokeLater(new Runnable()
        {
            public void run() 
            {
            URL url = null;
            try {
                url = new URL("file:///C:/PersonalWorkSpace/PrivateEyes/html/test.html");
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
            JEditorPane jEditorPane = new JEditorPane();
            jEditorPane.setEditable(false);

            JScrollPane jScrollPane = new JScrollPane(jEditorPane);

            HTMLEditorKit kit = new HTMLEditorKit();
            jEditorPane.setEditorKit(kit);

            StyleSheet styleSheet = kit.getStyleSheet();
            styleSheet.addRule("body {color:#000; font-family:times; margin: 4px;}");
            styleSheet.addRule("h1 {color: blue;}");
            styleSheet.addRule("h2 {color: #ff0000;}");

            setLayout(new BorderLayout());
            add(jEditorPane);


            try {
                jEditorPane.setPage(url);
            } catch (IOException e) {
                e.printStackTrace();
            }
            jEditorPane.setVisible(true);
            jScrollPane.setVisible(true);
            System.out.println("Browser Window Run");
            }
        });
    }
}

我的一些代码可能会有点不稳定/奇怪,但那是因为我尝试了一些东西,但留下了一些残余。

提前致谢。

【问题讨论】:

    标签: java html swing runnable


    【解决方案1】:

    删除 BrowserFrame() 前面的void。它被视为方法而不是构造函数。

    【讨论】:

    • 天哪!想一想我花了这么多时间添加和删除,这是我出于习惯添加的一个词。一旦计时器用完,将接受您的回答。
    • 我讨厌这种情况发生,但我通常会在此过程中发现一些错误,这些错误会在以后引起头痛 =)
    猜你喜欢
    • 1970-01-01
    • 2016-03-05
    • 1970-01-01
    • 1970-01-01
    • 2014-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-10
    相关资源
    最近更新 更多