开始:Applet尚未初始化

问题描述 投票:0回答:1

我查看了有关堆栈溢出的其他答案,但没有一个起作用。这是教科书中的代码,因此应该可以使用。我也用相同的默认包制作了类和html文件,所以我不明白问题所在:

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.awt.geom.*;
import javax.swing.*;


public class BannerApplet extends Applet
{
   public void init()
   {
      message = getParameter("message");
      String fontname = getParameter("fontname");
      int fontsize = Integer.parseInt(getParameter("fontsize"));
      delay = Integer.parseInt(getParameter("delay"));
      font = new Font(fontname, Font.PLAIN, fontsize);
      Graphics2D g2 = (Graphics2D) getGraphics();
      FontRenderContext context = g2.getFontRenderContext();
      bounds = font.getStringBounds(message, context);

      timer = new Timer(delay, new
         ActionListener()
         {
            public void actionPerformed(ActionEvent event)
            {
               start--;
               if (start + bounds.getWidth() < 0) 
                  start = getWidth();
               repaint();
            }
         });
   }

   public void start()
   {
      timer.start();
   }

   public void stop()
   {
      timer.stop();
   }

   public void paint(Graphics g)
   {
      g.setFont(font);
      g.drawString(message, start, (int)-bounds.getY());
   }

   private Timer timer;
   private int start;
   private int delay;
   private String message;
   private Font font;
   private Rectangle2D bounds;
}

html:

<applet code="BannerApplet.class" width="300" height="100">
<param name="message" value="Hello, World!"/>
<param name="fontname" value="Serif"/>
<param name="fontsize" value="64"/>
<param name="delay" value="10"/>
</applet>

在eclipse中的外观:(就目录而言。)enter image description here

java html eclipse swing applet
1个回答
0
投票

如果直接运行BannerApplet.html,是否已编译.java文件以创建.class文件?

除非.class文件可用,否则HTML不会选择Java代码。

否则也许有帮助;https://help.eclipse.org/kepler/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Ftasks%2Ftask-launching_java_applet.htm

© www.soinside.com 2019 - 2024. All rights reserved.