Java applet使用额外的随机字母呈现JLabel(和其他组件)

时间:2011-05-27 01:48:53

标签: java swing applet appletviewer

当我运行下面列出代码的applet时,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);
    }

}

以下是我看起来的截图:

enter image description here

2 个答案:

答案 0 :(得分:2)

确保创建Swing GUI组件&在美国东部时间更新。

E.G。 1(未经测试)

package com.evanmallory;

import java.awt.*;
import javax.swing.*;

public class TellTime extends JApplet {

    private JLabel mMessage;

    public TellTime() {
        SwingUtilities.invokeLater( new Runnable() {
            public void run() {
                mMessage = new JLabel("Set the clock to the given time.",
                    SwingConstants.CENTER);
                mMessage.setFont(new Font("Serif", Font.PLAIN, 36));
                getContentPane().add(mMessage);
            }
        });
    }
}

E.G。 2(在applet查看器中未经测试)

基于(摘自)camickr的示例,但调用setFont()包裹在Timer中,每半秒触发一次,然后调用repaint()

// <applet code="AppletBasic" width="300" height="100"></applet>
// The above line makes it easy to test the applet from the command line by using:
// appletviewer AppletBasic.java

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;

public class AppletBasic extends JApplet
{
    Timer timer;

    /**
     * Create the GUI. For thread safety, this method should
     * be invoked from the event-dispatching thread.
     */
    private void createGUI()
    {
        final JLabel appletLabel = new JLabel( "I'm a Swing Applet" );
        appletLabel.setHorizontalAlignment( JLabel.CENTER );

        ActionListener listener = new ActionListener() {
            Random random = new Random();
            public void actionPerformed(ActionEvent ae) {
                // determine a size between 12 & 36.
                int size = random.nextInt(24)+12;
                appletLabel.setFont(new Font("Serif", Font.PLAIN, size));
                // tell the applet to repaint
                repaint();
            }
        };
        timer = new Timer(500, listener);
        timer.start();

        add( appletLabel );
    }

    public void init()
    {
        try
        {
            SwingUtilities.invokeAndWait(new Runnable()
            {
                public void run()
                {
                    createGUI();
                }
            });
        }
        catch (Exception e)
        {
            System.err.println("createGUI didn't successfully complete: " + e);
        }
    }
}

因为我在这里。

<html>
<body>
<applet codebase="classes" code="com.evanmallory.TellTime.class"
  width=800 height=500>
    <param name="level" value="1"/>
    <param name="topic" value="TellTime"/>
    <param name="host" value="http://localhost:8080"/>
  </applet>
  </body>
</html>
  1. codebase="classes"看起来很可疑。如果这是在基于Java的服务器上,/classes(和/lib)目录将仅供服务器使用,并且applet无法访问。
  2. 代码属性应该是cass的完全限定名称,因此com.evanmallory.TellTime.class应为com.evanmallory.TellTime
  3. <param name="host" value="http://localhost:8080"/>我准备制作一个WAG,表明该值在部署时是不必要的或错误的。最好在运行时使用Applet.getDocumentBase()Applet.getCodeBase()确定。
  4. 顺便说一句 - 小程序在我最近运行最新Oracle Java的FF中运行良好。但是EDT问题是不确定的(随机的),所以这并不意味着什么。

答案 1 :(得分:1)

Swing教程总是在init()方法中创建GUI组件:

// <applet code="AppletBasic.class" width="400" height="400"></applet>
// The above line makes it easy to test the applet from the command line by using:
// appletviewer AppletBasic.java

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

public class AppletBasic extends JApplet
{
    /**
     * Create the GUI. For thread safety, this method should
     * be invoked from the event-dispatching thread.
     */
    private void createGUI()
    {
        JLabel appletLabel = new JLabel( "I'm a Swing Applet" );
        appletLabel.setHorizontalAlignment( JLabel.CENTER );
        add( appletLabel );
    }

    public void init()
    {
        try
        {
            SwingUtilities.invokeAndWait(new Runnable()
            {
                public void run()
                {
                    createGUI();
                }
            });
        }
        catch (Exception e)
        {
            System.err.println("createGUI didn't successfully complete: " + e);
        }
    }

}

请参阅:How to Make Applets