我怎么能有一个单独的KeyListener和JComponent类?

时间:2017-05-10 09:03:05

标签: java

    import java.awt.Graphics;
        import java.awt.image.BufferedImage;
        import java.io.File;
        import java.io.IOException;
        import java.net.URL;
        import javax.imageio.ImageIO;
        import javax.swing.JComponent;

        public class GameDraw extends JComponent {

            private int rows;
            private int columns;
            private int RWIDTH;
            private int RHEIGHT;
            private int manX;
            private int manY;
            private BufferedImage man;

            public GameDraw() {
                //Integer variables
                rows = 9;
                columns = 9;
                RWIDTH = 70;
                RHEIGHT = 70;
                manX = 350;
                manY = 0;

                URL resourceMan = this.getClass().getResource("/resources/man.png");

                try {
                    man = ImageIO.read(resourceMan);
                } catch (IOException e) {
                    System.out.println("Er ging iets mis met het laden van de afbeelding van de speler");
                }

                this.setFocusable(true);
            }

            public void paintComponent(Graphics g) {
                drawField(g);
                drawMan(g);
            }

            private void drawField(Graphics g) {
                int x = 0;
                int y = 0;
                for (int i = 0; i < rows; i++) {
                    for (int j = 0; j < columns; j++) {
                        g.drawRect(x, y, RWIDTH, RHEIGHT);
                        x += RWIDTH;
                    }

                    g.drawRect(x, y, RWIDTH, RHEIGHT);
                    x = 0;
                    y += RHEIGHT;
                }
            }

            private void drawMan(Graphics g) {
                g.drawImage(man, manX, manY, this);
            }

            public void moveMan(int x, int y) {
                manX = manX + x;
                manY = manY + y;


           repaint();
        }

}





import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class Man implements KeyListener {

    private int locationX;
    private int locationY;
    private GameDraw draw;

    public Man() {
        locationX = 350;
        locationY = 0;
        draw = new GameDraw();
        draw.addKeyListener(this);

    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        int key = e.getKeyCode();
        switch (key) {

            case KeyEvent.VK_UP:
                locationY = locationY + 80;
                draw.moveMan(locationX, locationY);
                break;

            case KeyEvent.VK_DOWN:
                locationY = locationY - 80;
                System.out.println("hey");
                draw.moveMan(locationX, locationY);
                break;

            case KeyEvent.VK_LEFT:
                locationX = locationX - 80;
                draw.moveMan(locationX, locationY);
                break;

            case KeyEvent.VK_RIGHT:
                locationX = locationX + 80;
                draw.moveMan(locationX, locationY);
                break;

            default:
                break;
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

}

我的问题:

将KeyListener添加到GameDraw类时,为什么Man类中的KeyListener和KeyEvent函数仍然不对应且有效?你能不能给我一个单独的Man类和GameDraw类,并能够在Man类中提交动作的方法?

编辑:这是UI类的JFrame类

package riddle;

import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;

class GameFrame extends JFrame {

    private JPanel p2;
    private JButton reset;
    private JButton re;
    private JButton r;
    private GameDraw component;

    public GameFrame() {
        this.setTitle("Riddle Man");
        this.setSize(719, 800);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        component = new GameDraw();
        component.setPreferredSize(new Dimension(300, 300));

        createPanel();
    }

    private void createPanel() {

        JPanel p2 = new JPanel();

        p2.setLayout(null);

        component.setBounds(0, 0, 800, 800);

        p2.add(component);
        add(p2);

    }

}

1 个答案:

答案 0 :(得分:0)

修改代码,以便将GameDraw附加到其他UI对象,这应该可以工作(骨架):

public class GameDraw extends JComponent {
   ...
}

public class Man implements KeyListener {
    ...
    private GameDraw draw;

    public Man(GameDraw draw) {
        ...
        this.draw = draw;
    }
    ...
}

class GameFrame extends JFrame {
  ...
  private GameDraw component;

  public GameFrame() {
    ...
    component = new GameDraw();
    Man man = new Man(component);
    component.addKeyListener(man);
    ...
  }
}