我需要为计算机科学课程编写一个applet。我需要从顶部和右侧滚动到屏幕上的五个歌曲标题,一个循环一次一个。我编写了我的代码并编译但是当我尝试在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>