3

コードが以下にリストされているアプレットを実行すると、 のテキストがJLabel正しく描画されません。ラベル テキストの上に余計な文字が重なって表示されます。

への呼び出しを省略した場合setFont()、レンダリングの問題は見られません。

アプレットはアプレット ビューアーでは正常に動作しますが、Chrome、Firefox、および IE 8 ではこれらのレンダリング アーティファクトが発生します。Windows XP で最新バージョンの Java 6 (rev. 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);
    }

}

これが私にとってどのように見えるかのスクリーンショットです:

ここに画像の説明を入力

4

2 に答える 2

2

SwingGUIコンポーネントが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の例に基づいていますが、0.5秒ごとに起動setFont()するaにラップされた呼び出しと、それに続く。への呼び出しがあります。Timerrepaint()

// <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. <param name="host" value="http://localhost:8080"/>私は、その値が展開時に不要または間違っているWAGを作成する準備ができています。実行時にApplet.getDocumentBase()またはを使用して決定することをお勧めしますApplet.getCodeBase()

ところで-アプレットは、最近のOracleJavaを実行している最近のFFで正常に機能しました。ただし、EDTの問題は不確定(ランダム)であるため、あまり意味がありません。

于 2011-05-27T01:53:12.157 に答える
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);
        }
    }

}

参照:アプレットの作成方法

于 2011-05-27T02:38:22.043 に答える