Java 小程序用额外的随机字母呈现 JLabel(和其他组件)

发布于 2024-11-09 17:11:27 字数 936 浏览 0 评论 0原文

当我运行下面列出的代码的小程序时,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:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

箹锭⒈辈孓 2024-11-16 17:11:27

确保 Swing GUI 组件已创建并可用美国东部时间 (EDT) 已更新。

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

EG 2(除小程序查看器外未在任何程序中进行测试)

基于(摘自)camickr 的示例,但调用了包含在 Timer 中的 setFont()每半秒触发一次,然后调用 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)目录将仅供服务器专用,并且小程序无法访问。
  2. code 属性应该是 cass 的完全限定名称,因此 com.evanmallory.TellTime.class 应为 com.evanmallory.TellTime
  3. 我准备制作一个 WAG,该值在部署时要么是不必要的,要么是错误的。最好在运行时使用 Applet.getDocumentBase()Applet.getCodeBase() 进行确定。

顺便说一句 - 这个小程序在我最近运行最近的 Oracle Java 的 FF 中运行良好。但 EDT 问题是不确定的(随机的),所以这没有多大意义。

Make sure Swing GUI components are created & updated on the EDT.

E.G. 1 (untested)

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 (untested in anything but applet viewer)

Based on (ripped from) camickr's example, but with a call to setFont() wrapped in a Timer that fires every half second, and a subsequent call to 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);
        }
    }
}

And since I'm here.

<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. The 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.
  2. The code attribute should be the fully qualified name of the cass, so com.evanmallory.TellTime.class should be com.evanmallory.TellTime.
  3. <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 using Applet.getDocumentBase() or Applet.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.

仙气飘飘 2024-11-16 17:11:27

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

}

请参阅:如何制作小程序

The Swing tutorial always creates the GUI components in the init() method:

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

}

See: How to Make Applets

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文