Java Applet中的面板不显示

时间:2014-02-02 15:56:49

标签: java html applet awt

我有一个嵌入HTML页面的Java Applet(java.applet.Applet)。有一个Control类,extends Applet和一个Panel(java.awt.Panel)与Applet的形状和大小相同,可以在屏幕上显示所有内容。

当我从NetBeans IDE运行项目时,一切都按预期显示:Applet打开并包含一个带有我的初始屏幕的面板。

当我从我网站上的HTML页面运行项目时,我看到一个空白的白页。图像执行加载;在谷歌浏览器中,我可以右键单击该页面并单击“检查元素”,整个游戏显示并正常运行。您可以在http://www.philipthegreat.com/control.html

查看该页面

我的想法:Panel正在后面显示而不是 Applet前面。

以下是Control和SplashScreen类中的代码。

控制

public class Control extends Applet {

private Panel displayPanel;


 @Override
 public void init() {
    setFocusable(true);
 }

 @Override
 public void start() {
    displayPanel = new SplashScreen(this);
    add(displayPanel);
    displayPanel.requestFocus();
 }

 public void setDisplayPanel (Panel displayPanel) {
    remove(this.displayPanel);
    this.displayPanel = displayPanel;
    add(this.displayPanel);
    this.displayPanel.requestFocus();
 }

}

闪屏

public SplashScreen(Control control) {
    setLayout(null);
    setSize(800,600);
            setBackground(Color.BLUE);
    this.control = control;
    init();
}

public void init() {
    try {
                splashScreen = ImageIO.read(getClass().getResourceAsStream("/images/SplashScreen.png"));
    } catch (Exception e) {
        e.printStackTrace();
    }
    addMouseListener((MouseListener) this);
}

public void destroy() {

}

@Override
public void paint(Graphics g) {
            g.drawImage(splashScreen, 0, 0, null);
}

由于我对Java比较陌生,所以我们都会感激任何指针。

1 个答案:

答案 0 :(得分:2)

尝试使用Control.java而不是Control.java

//<applet code="Control.java" height=200 width=500></applet>
public class Control extends JApplet {
    private Panel displayPanel;

    @Override
    public void init() {
        setFocusable(true);
    }

    @Override
    public void start() {
        displayPanel = new SplashScreen(this);
        add(displayPanel);
        displayPanel.requestFocus();
    }

    public void setDisplayPanel(Panel displayPanel) {
        remove(this.displayPanel);
        this.displayPanel = displayPanel;
        add(this.displayPanel);
        this.displayPanel.requestFocus();
    }
}