简体   繁体   中英

Java applet pane movable by arrow keys?

如何使Java applet不受限制或设置为一定的大小,我可以使用箭头键移动事物(而不是窗口),并且图像和内容将保留在原处?

You can use KeyListeners to do this task. Like this :-

class MyKeyListener extends KeyAdapter{
    public void keyPressed(KeyEvent e){
        switch (e.getKeyCode()){
            case KeyEvent.VK_LEFT:
            leftKey = true;
            break;
            case KeyEvent.VK_RIGHT:
            rightKey = true;
            break;
            case KeyEvent.VK_UP:
            upKey = true;
            break;
            case KeyEvent.VK_DOWN:
            downKey = true;
            break;
        }
    }
    public void keyReleased(KeyEvent e){
        switch (e.getKeyCode()){
            case KeyEvent.VK_LEFT:
            leftKey = false;
            break;
            case KeyEvent.VK_RIGHT:
            rightKey = false;
            break;
            case KeyEvent.VK_UP:
            upKey = false;
            break;
            case KeyEvent.VK_DOWN:
            downKey = false;
            break;
        }
    }

This is only a way how can you use the KeyListner and events. You can do it in your way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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