Java 小程序用额外的随机字母呈现 JLabel(和其他组件)
当我运行下面列出的代码的小程序时,JLabel
的文本无法正确绘制。标签文本上方叠加了额外的垃圾字符。
如果我省略对 setFont()
的调用,我不会看到任何渲染问题。
该小程序在 appletviewer 中运行良好,但在 Chrome、Firefox 和 IE 8 中出现这些渲染问题。我在 Windows XP 上运行最新版本的 Java 6(修订版 25)。该问题似乎总是在 Chrome 中出现,而在 Firefox 中则间歇性出现。
您对可能导致这种情况的原因有任何想法吗?我想我正在做一些愚蠢的事情。
我已将编译后的小程序发布在这里: http://evanmallory.com/bug-demo/ 。
package com.evanmallory;
import java.awt.*;
import javax.swing.*;
public class TellTime extends JApplet {
private JLabel mMessage;
public TellTime() {
mMessage = new JLabel("Set the clock to the given time.",
SwingConstants.CENTER);
mMessage.setFont(new Font("Serif", Font.PLAIN, 36));
getContentPane().add(mMessage);
}
}
这是我的屏幕截图:
When I run the applet whose code is listed below, the text of the JLabel
does not draw properly. There are extra garbage characters superimposed on top of the label text.
If I omit the call to setFont()
, I don't see any rendering problems.
The applet runs fine in the appletviewer, but has these rendering artifacts in Chrome, Firefox, and IE 8. I'm running the latest version of Java 6 (rev. 25) on Windows XP. The problem seems to always occur in Chrome, and be intermittent in Firefox.
Do you have any ideas about what could be causing this? I suppose I'm doing something stupid.
I've posted the compiled applet here: http://evanmallory.com/bug-demo/.
package com.evanmallory;
import java.awt.*;
import javax.swing.*;
public class TellTime extends JApplet {
private JLabel mMessage;
public TellTime() {
mMessage = new JLabel("Set the clock to the given time.",
SwingConstants.CENTER);
mMessage.setFont(new Font("Serif", Font.PLAIN, 36));
getContentPane().add(mMessage);
}
}
Here's a screenshot of what it looks like for me:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
确保 Swing GUI 组件已创建并可用美国东部时间 (EDT) 已更新。
EG 1(未经测试)
EG 2(除小程序查看器外未在任何程序中进行测试)
基于(摘自)camickr 的示例,但调用了包含在
Timer
中的setFont()
每半秒触发一次,然后调用repaint()
。既然我在这里。
codebase="classes"
看起来很可疑。如果这是在基于 Java 的服务器上,则/classes
(和/lib
)目录将仅供服务器专用,并且小程序无法访问。com.evanmallory.TellTime.class
应为com.evanmallory.TellTime
。我准备制作一个 WAG,该值在部署时要么是不必要的,要么是错误的。最好在运行时使用
Applet.getDocumentBase()
或Applet.getCodeBase()
进行确定。顺便说一句 - 这个小程序在我最近运行最近的 Oracle Java 的 FF 中运行良好。但 EDT 问题是不确定的(随机的),所以这没有多大意义。
Make sure Swing GUI components are created & updated on the EDT.
E.G. 1 (untested)
E.G. 2 (untested in anything but applet viewer)
Based on (ripped from) camickr's example, but with a call to
setFont()
wrapped in aTimer
that fires every half second, and a subsequent call torepaint()
.And since I'm here.
codebase="classes"
looks suspicious. If this were on a Java based server, the/classes
(and/lib
) directory would be for the exclusive use of the server, and would not be accessible to an applet.com.evanmallory.TellTime.class
should becom.evanmallory.TellTime
.<param name="host" value="http://localhost:8080"/>
I am prepared to make a WAG that that value is either unnecessary or wrong for time of deployment. It is better to determine at run-time usingApplet.getDocumentBase()
orApplet.getCodeBase()
.BTW - The applet worked fine in my recent FF running a recent Oracle Java. But EDT problems are non determinate (random), so that does not mean much.
Swing 教程始终在 init() 方法中创建 GUI 组件:
请参阅:如何制作小程序
The Swing tutorial always creates the GUI components in the init() method:
See: How to Make Applets