java - 在 Swing 中显示 HTML

标签 java html swing

我正在做一个从服务器加载 HTML 文件并在 swing 中显示它们的项目。

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 的新手,所以如果我看起来很笨或者不知道自己在做什么,那是因为我是。

最佳答案

我试图访问错误的文件。

正确的代码:

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

关于java - 在 Swing 中显示 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13777377/

相关文章:

javascript - 如何检查图像是否脏 PhotoEditorSDK

php - 使用 php 设置 <div> 属性的正确语法

java - 如何强制 BoxLayout 和 JScrollPane 限制内容大小?

java - 使用 java-apns(notnoop) 发送 ios voip 通知

java - 错误: org. hibernate.PropertyNotFoundException : no appropriate constructor in class: com. a.offering.dto.SellerDetailsDto

jquery - 我想让图像在 Bootstrap 中粘在一起,但我做不到

java - 填写现有字段后添加 "New"字段

java - 在 JBoss Forge 中创建 oneToMany 字符串字段

java - 使用 Java 检索网页内容与屏幕副本获取的内容不符

java - JTextArea 的大小