java html 显示_java – 在Swing中显示HTML

我正在开发一个类似的项目来从服务器加载

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很新,所以如果我看起来很愚蠢或不知道我在做什么,那是因为我.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值