0

我需要为计算机科学课程编写一个小程序。我需要有五个歌曲标题从顶部和右侧滚动到屏幕上,一次一个循环。我编写了我的代码并编译了它,但是当我尝试在 appletviewer 中运行它时,我得到一个空白窗口,显示 applet 未初始化。我假设这不是主代码的问题,它可能是 start() 方法中的问题。这是代码:

import java.awt.*;
public class SongTitles extends java.applet.Applet implements Runnable
{
String song[] = {"Watch The Corners","Where Is My Mind","Different World","Seven Nation Army","Nowhere Man"};
int[] y = new int[] {0,0,0,0,0};
int[] x = new int[] {200,200,200,200,200};
boolean[] cycle = new boolean[5];
Thread runner;

public void start()
{
    if (runner == null)
    {
        runner = new Thread(this);
        runner.start();
    }
}


public void paint (Graphics g)
{
for(int i = 0; i < 5; i++)
{
        g.drawString(song[i], x[i], y[i]);
}
}

public void run()
{
cycle[0] = true;

for(int c = 0; c < 5; c++)
{

        while(cycle[c] = true)
        {
        if (y[c] < 200)
        {
                y[c] += 2;
                repaint();
                try 
                {
                runner.sleep(50);
                }
                catch (InterruptedException e) { }
    }

        if (y[c] == 200 && x[c] < 400)
        {
                x[c] += 2;
                repaint();
                try 
                {
                    runner.sleep(50);
                }
                catch (InterruptedException e) { }
            }

        if (x[c] == 400)
        {
                x[c] = 200;
            y[c] = 0;
                repaint();
        cycle[c] = false;
        cycle[(c + 1)] = true;
                try 
                {
                    runner.sleep(50);
                }
                catch (InterruptedException e) { }
            }

    }

    if(c < 5)
    {
        c++;
    }

    if(c == 5)
    {
        c = 0;
    }
    }
  }
}

这是运行它的 html 文件:

<html>
<body>
<applet code = "SongTitles.class" width=400 height=400>
</applet>
</body>
</html> 
4

0 回答 0