HTML 不再适用于 JLabel(和其他组件) [英] HTML no longer working in JLabel (and other components)

查看:15
本文介绍了HTML 不再适用于 JLabel(和其他组件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自己编写的 Java 小程序,并且已经运行了一段时间.在小程序中,我有一堆在文本内容中使用 HTML 的 JLabel(这是允许的,并且已经工作了多年).我使用 HTML 的主要原因是允许在 JLabels 中换行.

I have a java applet that I've written and have been running it for quite a while. In the applet I have a bunch of JLabels that use HTML in their text content (which is allowed, and has been working for years). The main reason I use HTML is to allow line breaks in JLabels.

问题: 最近,包含 HTML 的 JLabel(有些没有)停止显示其文本.我认为这可能与最近的 Java 更新(Java SE 6 更新 22,2010 年 10 月 12 日)有关,但并非 100% 肯定,但问题似乎确实是从那时开始的.也许已经引入了一个错误?或删除了功能?

The issue: fairly recently the JLabels that have HTML in them (some don't) stopped displaying their text. I think it might have something to do with the recent java update (Java SE 6 Update 22, on 2010-Oct-12) not 100 percent sure, but the problems did seem to start around then. Maybe a bug has been introduced? or feature removed?

我尝试使用 JEditorPane,但在内容为 HTML 时似乎遇到了同样的问题.

I tried with a JEditorPane and seemed to have the same issues when the content was HTML.

还有一点需要注意的是,第一次加载小程序(第一次启动 Java 运行时)时,它可以正常工作,但如果刷新网页,则会出现上述问题.

Also something important to note is that the first time you load the applet (first time your java runtime starts) it works FINE, but if you refresh the webpage then it has the issues as described.

有人遇到过类似的问题吗?有人有任何见解吗?还是我只是在做一些愚蠢的事情?

Does anyone have similar issues? anyone have any insights? or am I just doing something dumb?

我制作了一个非常简单的测试小程序,我可以 100% 规律地重现问题(请记住,第一次运行时加载它会正常工作,只有连续刷新才会导致问题):

I made a very simple test applet and I can reproduce the issues with 100% regularity (remembering that the first time the runtime loads it will work fine, only successive refreshes will cause the issues):

[HelloWorldApplet.java]

[HelloWorldApplet.java]

import javax.swing.*;

public class HelloWorldApplet extends JApplet
{
    public void init()
    {
        this.add(new JLabel("Hello Everybody (Text)"), java.awt.BorderLayout.NORTH);
        this.add(new JLabel("<html><body>Hello Everybody (HTML)</body></html>"), java.awt.BorderLayout.SOUTH);
    }
}

[HellowWorldApplet.html]

[HellowWorldApplet.html]

<html>
<title>A Test</title>
<body>
    <applet code="HelloWorldApplet.class" width="320" height="120">
        You need Java
    </applet>
</body>
</html>

附注.我一直主要使用 Chrome 进行测试,但我也曾在 IE 8 中进行过简单的尝试.此外,我(显然)安装了最新的 Java SE 6 更新 22 作为我的运行时,并为我的 JDK 安装了匹配的更新.我使用简单的编译了上面的测试小程序:javac HelloWorldApplet.java"

PS. I've been mainly testing using Chrome but I did breifly try in IE 8 also. Also I (obviously) have the latest Java SE 6 update 22 installed as my run time, and have the matching update for my JDK. I compile the above test applet using simply: "javac HelloWorldApplet.java"

推荐答案

浪费了一个下午寻找原因,请参阅以下错误报告 http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6993691

Wasted an afternoon looking for the cause of this, see the following bug report http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6993691

错误报告中的解决方法是:

The workaround in the bug report is:

import javax.swing.*;
import javax.swing.text.html.parser.ParserDelegator;

public class HelloWorldApplet extends JApplet {
    public void init() {
       ParserDelegator workaround = new ParserDelegator();
       applet.add(new JLabel("Hello Everybody (Text)"), java.awt.BorderLayout.NORTH);
       applet.add(new JLabel("<html>Hello Everybody (HTML)</html>"),  java.awt.BorderLayout.SOUTH);
    }
}

这篇关于HTML 不再适用于 JLabel(和其他组件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆