如何在JFrame中的对象上实现KeyListener?

时间:2019-06-11 03:54:33

标签: java swing keylistener

我正在尝试用Java在学校项目中制作Frogger,但是在为实际的青蛙角色设置KeyListener时遇到很多困难。

我尝试设置键绑定,请求JPanel和JFrame的焦点,在启动字符的位置移动,但似乎没有任何作用。这是我尝试的残余。

这是运行我的游戏的程序。

import javax.swing.*;

public class Frogger
{
 JFrame frame = new JFrame("Frogger");
 CPT c = new CPT();
 public Frogger()
 {
  frame.setBounds(0,0,700,500);
  frame.setResizable(false);
  frame.setVisible(true);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.add(c);
 }

 public static void main(String[] args){
   new Frogger();
 }

}

主要游戏

public CPT() {

    setLayout(new BorderLayout());
    label = new JLabel("Frogger");
    frame1 = new JFrame("Main");
    label.setFont(new Font("Serif", Font.BOLD,50));
    label.setBounds(275,10,250,250);
    button1 = new JButton("PLAY");
    button1.setBounds(300,350,100,50);
    button1.setOpaque(false);
    button1.setVisible(true);
    this.setOpaque(false);
    this.setLayout(null);
    this.add(label);


    button1.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e){
        frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame1.setVisible(true);
        frame1.setSize(700,500);
        frame1.setResizable(false);
        frame1.setFocusable(false);
        button1.setVisible(false);
        frame1.add(new TrainCanvas());
        frame1.add(p1);
        p1.requestFocus();
      } 
    });
    this.add(button1); }

这是TrainCanvas类,它绘制游戏中的汽车以及青蛙

  class TrainCanvas extends JComponent
 {
    private int lastX = 0;
    private int lastX_1 = 0;
    private int lastX_2 = 0;


    public TrainCanvas() {
      Thread animationThread = new Thread(new Runnable() {
        public void run() {
          while (true) {
            repaint();
            try {Thread.sleep(10);} catch (Exception ex) {}
          }
        }
      });

      animationThread.start();
    }

    public void paintComponent(Graphics g) {
      Graphics2D gg = (Graphics2D) g;

      //Draws Train 1
      int w = getWidth();
      int h = getHeight();

      int trainW_1 = 100;
      int trainH_1 = 5;
      int trainSpeed_1 = 3;

      int x = lastX + trainSpeed_1;

      if (x > w + trainW_1) {
        x = -trainW_1;
      }

      gg.setColor(Color.BLACK);
      gg.fillRect(x, h/2 + trainH_1, trainW_1, trainH_1);
      lastX = x;
      Graphics2D g3 = (Graphics2D) g;
      frog = new Rectangle(f_x,f_y,25,25);
      g3.fill(frog);
      g3.setColor(Color.GREEN);
   }
}

最后,关键监听器

    public void keyPressed(KeyEvent e)
    {
      if(e.getKeyCode()== KeyEvent.VK_UP)
      {
        CPT.f_y -= 100;
        repaint();
      }
      else if(e.getKeyCode()== KeyEvent.VK_RIGHT)
      {
        CPT.f_x += 100;
        repaint();
      }
      else if(e.getKeyCode() == KeyEvent.VK_LEFT)
      {
        CPT.f_x -= 100;
        repaint();
      }
      else if(e.getKeyCode()==KeyEvent.VK_DOWN)
      {
        CPT.f_y += 100;
        repaint();
      }
      else {}
    }
    public void keyReleased(KeyEvent e) {}
    public void keyTyped(KeyEvent e) {} 
}

程序运行得很好,没有给我任何错误,这很麻烦。每当它到达游戏主窗口时,所有按键似乎都不起作用。

0 个答案:

没有答案