java错误中的关键事件

时间:2014-12-24 17:09:56

标签: java image swing paintcomponent keylistener

为什么我收到此错误,即bruh.setLocation(bruh.x + 2,bruh.y);错误是x和y变量。我该如何解决这个问题?

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        ImageIcon ii = new ImageIcon(this.getClass().getResource("bruh.png"));
        bruh = ii.getImage();
        Graphics2D g2d = (Graphics2D)g; 
        g2d.drawImage(bruh, x, y, this);
    }

    public void keyTyped(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_RIGHT)
        {
            bruh.setLocation(bruh.x + 2, bruh.y);
        }
    }    

    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_LEFT)
        {                
        }    
    }    

    public void keyReleased(KeyEvent e) {           
    }
}

1 个答案:

答案 0 :(得分:1)

错误是bruhImage而Image没有setLocation方法