KeyListener not working with PaintComponent

允我心安 提交于 2019-12-02 15:58:57

问题


My program is supposed to wait for either the left or right arrow key to be pressed, then change a value so the next time PaintComponent is updated, the screen looks different. However, the screen doesn't change when I run the program.

Here are the variable declarations:

static KeyListener listen;

public static int slide = 0;

Here's the KeyListener declaration in the main:

listen = new KeyListener() {
        public void keyPressed(KeyEvent arg0) {
            if(arg0.getKeyCode() == KeyEvent.VK_LEFT && slide>0) {
                slide--;
                System.out.println("Left pressed. Slide is " + slide);
            }
            else if(arg0.getKeyCode() == KeyEvent.VK_RIGHT) {
                slide++;
                System.out.println("Right pressed. Slide is " + slide);
            }
        }
        public void keyReleased(KeyEvent arg0) {}
        public void keyTyped(KeyEvent arg0) {}

    };

Here is the methods used for the painting:

public void paintComponent(Graphics g){
        super.paintComponent(g);
        this.setBackground(new Color(0, 0, 20));
        g.drawImage(exitButton, 20, 20, null);
        drawBoard(g);
    }



    public void drawBoard(Graphics g){

        if(slide == 0) {
            String[] text0 = {"This is the test",
                    "for the Presenter application",
                    "Here is a nicely framed picture of the Berlin Wall."
                    };
            makeTextBox(text0);
            g.drawImage(textBox, textX, textY,(boxW)*2,(boxH)*2, null);
            g.setColor(Color.blue);
            g.fillRect(wallX-10, wallY-10, wallW+20, wallH+20);
            g.drawImage(wall, wallX, wallY, null);
        }
        else if(slide == 1) {
            String[] text1 = {"Here is the second page...",
                                "Welcome."};
            makeTextBox(text1);
            g.drawImage(textBox, textX, textY,(boxW)*2,(boxH)*2, null);
        }

    }

When I run the program, the program prints the if(slide == 0) case, but when I press the arrow keys, nothing changes on screen.


回答1:


You need to add the key listener to the component before it will be called:

addKeyListener(new KeyListener() {
    /* ... methods as before ... */
});

You also need to make the component focusable and give it the focus:

setFocusable(true);
requestFocusInWindow();

Finally, if you want to redraw the screen when the key is pressed, you have to call the component's repaint() method in the key press handler.




回答2:


Don't use a KeyListener it will now work when the component doesn't have focus!

Swing was designed to be used with Key Bindings. See Motion Using the Keyboard for more information and examples the use Key Bindings.



来源:https://stackoverflow.com/questions/23369447/keylistener-not-working-with-paintcomponent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!