繁体   English   中英

小程序中未显示按钮

[英]Button not displaying in applet

以下是我的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它会按预期暂停游戏。 但是,它不会在屏幕上显示按钮。 我试图寻找无济于事的解决方案。 到底是什么情况?

对我来说效果很好...

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 ...

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

更新

我遇到的主要问题是:

在转义键处理程序中,如果direction为0,则pause选项将永远不会激活。

case KeyEvent.VK_ESCAPE:
    direction = -direction;

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

我必须要做的另一件事是调用revalidate ...

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM