【发布时间】:2017-01-22 08:50:11
【问题描述】:
我是小程序编程的新手。 当我在小程序查看器中运行我的代码时它可以工作,但是当我在浏览器中运行它时它不起作用(它显示一个空白的白色网页) 这是我的代码。
import java.awt.*;
import java.applet.*;
public class BannerApplet extends Applet implements Runnable
{
String msg= "A simple moving Banner";
Thread t=null;
int state;
boolean stopFlag;
public void init()
{
setBackground(Color.cyan);
setForeground(Color.red);
}
public void start()
{
t=new Thread(this);
stopFlag=false;
t.start();
}
public void run()
{
char ch;
for(int i=0;i<100;i++)
{
try
{
repaint();
Thread.sleep(250);
ch=msg.charAt(0);
msg=msg.substring(1,msg.length());
msg+=ch;
if(stopFlag)
break;
}
catch(InterruptedException e){
}
}
}
public void stop()
{
stopFlag=true;
t=null;
}
public void paint(Graphics g)
{
g.drawString(msg,50,30);
showStatus("this is a message box");
}
}
这是我的 html 代码。
<!DOCTYPE html>
<html>
<title>
applet window
</title>`
<body>
<applet code="BannerApplet.class" width=300 height=100>
</applet>
</body>
</html>
【问题讨论】:
-
帮我解决这个问题
-
一些浏览器不支持小程序 - 你应该很高兴它在你学到的东西的小程序查看器上运行 - 然后将其转换为常规的 GUI 程序
-
1) 为什么要编写小程序?如果是老师指定的,请参考Why CS teachers should stop teaching Java applets。 2) 见Java Plugin support deprecated 和Moving to a Plugin-Free Web。 ..
-
.. 3) 为什么使用 AWT?请参阅 this answer 了解放弃 AWT 组件以支持 Swing 的许多充分理由。