尝试使用键侦听器,但形状不会移动



我已经添加了一个键侦听器,以尝试在我按下右箭头键时获得向右移动的形状。但这行不通。我不太会用keylistner。谁能帮帮我?

这是代码:

package walkingman;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class WalkingMan extends JPanel implements KeyListener{
    int x = 0;
    int y = 0;
    @Override
    public void paint(Graphics g){
        super.paint(g);
        g.fillOval(x, y, 150, 150);
    }
    public static void main(String[] args) throws InterruptedException {
        JFrame frame = new JFrame ("Walking Man");
        frame.setSize(1080,720);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        WalkingMan game = new WalkingMan();
        frame.add(game);
        while (true){
            game.repaint();
            game.keyPressed(e);
        }
    }
    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_RIGHT){
            x++;
        }
        repaint();
    }
    @Override
    public void keyReleased(KeyEvent e) {
    }
    @Override
    public void keyTyped(KeyEvent e) {
    }
}

你的代码有几个问题

  1. 永远不要将KeyListener添加到面板中。
  2. JPanelKeyListener只有在可聚焦的情况下才能工作&也集中。
  3. 覆盖paintComponent而不是paint
  4. 方法结束时调用setVisible
  5. 去掉整个while循环,它只会引起问题。
  6. 使用KeyBindings代替KeyListener s

修复了没有键绑定的代码:

import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class WalkingMan extends JPanel implements KeyListener {
    int x = 0;
    int y = 0;
    @Override
    public void paintComponent(Graphics g) { // Overide paintComponent, not paint
        super.paintComponent(g);
        g.fillOval(x, y, 150, 150);
    }
    public WalkingMan() { // Class Constructor
        setFocusable(true); // KeyListeners only work if the component is focusable
        addKeyListener(this); // Add the KeyListener implemented by this class to the instance
    }
    public void createAndShowGUI() {
        JFrame frame = new JFrame("Walking Man");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        WalkingMan game = new WalkingMan();
        frame.add(game);
        frame.setSize(1080, 720);
        frame.setVisible(true); // Call setVisible after adding the components
        game.requestFocusInWindow(); // Request focus for the panel
    }
    public static void main(String[] args) throws InterruptedException {
        new WalkingMan().createAndShowGUI();
    }
    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            x++;
        }
        repaint();
    }
    @Override
    public void keyReleased(KeyEvent e) {
    }
    @Override
    public void keyTyped(KeyEvent e) {
    }
}

在显示之前设置好frame,并删除仍在运行的while循环。

 public static void main(String[] args) throws InterruptedException {
        JFrame frame = new JFrame ("Walking Man");
        frame.setSize(1080,720);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        WalkingMan game = new WalkingMan();
        frame.add(game);
        frame.setVisible(true);//Call visible method here
    }

为什么不像这样以一种更清晰的方式重构你的代码呢

import javax.swing.*;
import java.awt.*;
import javax.swing.border.LineBorder;
import java.awt.Color;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class WalkingMan  extends JFrame{
    EmilsKeyClass keyBoard = new EmilsKeyClass();
    public WalkingMan (){
        add(keyBoard,BorderLayout.CENTER);
        keyBoard.addKeyListener(new KeyAdapter(){
                @Override
                public void keyPressed(KeyEvent e){ 
                    if(e.getKeyCode()== KeyEvent.VK_ENTER){
                        x++;
                        repaint();
                    }
                }
            });
        keyBoard.setFocusable(true);
    }
    public static void main(String [] args){
        WalkingMan  frame = new WalkingMan ();
        frame.setTitle("Walking Man");
        frame.setSize(1080,720);   
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
    public class EmilsKeyClass extends JPanel{
        int x = 0;
        int y = 0;
        public EmilsKeyClass(){            
        }
        @Override
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            super.paint(g);
            g.fillOval(x, y, 150, 150);
            //your code
        }  
    }
}

相关内容

  • 没有找到相关文章

最新更新