按钮不在applet中显示

时间:2012-10-11 01:31:39

标签: java applet awt keylistener runnable

以下是我的applet游戏的精简代码:

import java.applet.Applet;
import java.awt.event.*;
import java.awt.*;

public class Game extends Applet implements KeyListener, Runnable {
    Button options = new Button("Options");
    Thread thread = new Thread(this);

    public void init() {
        addKeyListener(this);
        thread.start();
    }

    public void paint(Graphics graphics) {
        // draw stuff
    }

    public void run() {
        try {
            while (true) {
                thread.sleep(40);
                repaint();
            }
        } catch (InterruptedException exception) {}
    }

    public void keyPressed(KeyEvent keyEvent) {
        switch (keyEvent.getKeyCode()) {
        case KeyEvent.VK_ESCAPE:
            // pause game
            add(options);
        }
    }

    public void keyReleased(KeyEvent keyEvent) {}
    public void keyTyped(KeyEvent keyEvent) {}
}

我的游戏按照预期运行。但是,当用户按下Esc时,我想暂停游戏并显示选项按钮。

问题在于,当我按Esc时,它会按预期暂停游戏。但是它不会在屏幕上显示按钮。我试图寻找一个无济于事的解决方案。究竟发生了什么?

1 个答案:

答案 0 :(得分:1)

对我来说效果很好......

public class TestApplet02 extends Applet implements KeyListener, Runnable {

    Button options = new Button("Options");
    Thread thread = new Thread(this);
    int y = 0;

    public void init() {
        thread.start();
    }

    @Override
    public void start() {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                setLayout(new BorderLayout());
                addKeyListener(TestApplet02.this);
            }
        });
    }

    public void paint(Graphics graphics) {
        super.paint(graphics);
        Graphics2D g2d = (Graphics2D) graphics;
        y++;
        if (y > getHeight()) {
            y = 0;
        }
        g2d.drawLine(0, y, getWidth(), y);
    }

    public void run() {
        try {
            while (true) {
                thread.sleep(40);
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        repaint();
                    }
                });
            }
        } catch (InterruptedException exception) {
        }
    }

    public void keyPressed(KeyEvent keyEvent) {
        switch (keyEvent.getKeyCode()) {
            case KeyEvent.VK_ESCAPE:
                // pause game
                add(options);
                invalidate();
                revalidate();
        }
    }

    public void keyReleased(KeyEvent keyEvent) {
    }

    public void keyTyped(KeyEvent keyEvent) {
    }
}

从提供的TrashGod链接...

  

在applet中,必须从init启动GUI创建任务   使用invokeAndWait的方法;否则,init可能会在GUI之前返回   已创建,这可能会导致Web浏览器启动时出现问题   小程序。在任何其他类型的程序中,调度GUI创建任务   通常是初始线程做的最后一件事,所以它不是   无论是使用invokeLater还是invokeAndWait。

<强>已更新

我遇到的主要问题是:

在你的转义键处理程序中,如果direction为0,暂停选项将永远不会激活...

case KeyEvent.VK_ESCAPE:
    direction = -direction;

    if (direction < 0) {
        add(options);
    } else {
        remove(options);
    }

我要做的另一件事就是致电revalidate ......

invalidate();
revalidate();
repaint();