在 Swing 中显示 HTML

Posted

技术标签:

【中文标题】在 Swing 中显示 HTML【英文标题】:Displaying HTML in Swing 【发布时间】:2012-11-26 11:23:51 【问题描述】:

我正在从事一个从服务器加载 html 文件并在摇摆中显示它们的项目。

import java.io.*;
import java.net.*;
import java.util.regex.*;
import javax.swing.*;


public class webloader 
    public static void loadcode()
        URL url = null;
        try 
            url = new URL("web"+File.separator+web.url+File.separator+"index.html");
         catch (MalformedURLException e) 
            e.printStackTrace();
        
        URLConnection con = null;
        try 
            con = url.openConnection();
         catch (IOException e) 
            e.printStackTrace();
        
        Pattern p = Pattern.compile("text/html;\\s+charset=([^\\s]+)\\s*");
        Matcher m = p.matcher(con.getContentType());
        String charset = m.matches() ? m.group(1) : "ISO-8859-1";
        Reader r = null;
        try 
            r = new InputStreamReader(con.getInputStream(), charset);
         catch (UnsupportedEncodingException e) 
            e.printStackTrace();
         catch (IOException e) 
            e.printStackTrace();
        
        StringBuilder buf = new StringBuilder();
        while (true) 
          int ch = 0;
        try 
            ch = r.read();
         catch (IOException e) 
            e.printStackTrace();
        
          if (ch < 0)
            break;
          buf.append((char) ch);
        
        String str = buf.toString();
        JFrame mainframe = new JFrame(web.url);
        mainframe.setSize(800, 750);
        mainframe.setResizable(false);
        JPanel website = new JPanel();
        JLabel webcontent = new JLabel(str);
        website.add(webcontent);
        mainframe.add(website);
        mainframe.setVisible(true);
    

错误:

Loading test.com
Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation
roblem:
        Syntax error on token ""web"", delete this token

        at webloader.loadcode(webloader.java:11)
        at web$1.actionPerformed(web.java:46)
        at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
        at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Sou
ce)
        at java.awt.Component.processMouseEvent(Unknown Source)
        at javax.swing.JComponent.processMouseEvent(Unknown Source)
        at java.awt.Component.processEvent(Unknown Source)
        at java.awt.Container.processEvent(Unknown Source)
        at java.awt.Component.dispatchEventImpl(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Window.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
        at java.awt.EventQueue.access$200(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Sou
ce)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Sou
ce)
        at java.awt.EventQueue$4.run(Unknown Source)
        at java.awt.EventQueue$4.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Sou
ce)
        at java.awt.EventQueue.dispatchEvent(Unknown Source)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.run(Unknown Source)

我对 Java 很陌生,所以如果我看起来很愚蠢或不知道自己在做什么,那是因为我是。

【问题讨论】:

"Error: Unresolved compilation problem:":您正在尝试运行无法编译的代码。 永远不要这样做。而是先尝试编译它,然后如果仍然感到困惑,请向我们显示编译器的错误消息,而不是JVM的错误消息。 没有编译错误, 即使上面的错误也能告诉你哪里出了问题;您正在使用一个名为 web 的变量,该变量未声明(据我所知)。 JAR 导出完成但出现警告。有关其他信息,请参阅详细信息。导出时出现编译警告:browser/src/updated.java 导出时出现编译警告:browser/src/Checkupdate.java 导出时出现编译警告:browser/src/web.java 如果您要求志愿者帮助您,“正确的语法”非常重要。我们通常感谢您为使代码更容易理解所做的努力,尤其是当您要求我们努力帮助您时。我认为我们要求您清理这些并让您的代码符合标准并不过分。 【参考方案1】:

我试图访问错误的文件。

正确代码:

import java.io.*;
import java.net.*;
import java.util.regex.*;
import javax.swing.*;


public class webloader 
    static JComponent page;
    public static void loadcode()
        JEditorPane jep = new JEditorPane();
         jep.setEditable(false);   

         try 
           jep.setPage("http://(server):(port)/" + web.url);
         
         catch (IOException e) 
           jep.setContentType("text/html");
           jep.setText("<html>Could not load webpage</html>");
          

         JScrollPane scrollPane = new JScrollPane(jep);     
         JFrame f = new JFrame(web.url);
         f.getContentPane().add(scrollPane);
         f.setSize(512, 342);
         f.show();
    

【讨论】:

以上是关于在 Swing 中显示 HTML的主要内容,如果未能解决你的问题,请参考以下文章

Java:Swing 中的 HTML/CSS,内联显示不起作用

在 Swing 中显示动画 BG

在 Swing 应用程序中渲染 html

java swing JTable显示问题

java swing编程:如何刷新动态显示的内容并生成滚动条显示

在swing组件中显示时所使用的字体可以用啥方法来设置