简体   繁体   English

应用程序不与箭头键配合

[英]application not cooperating with arrow keys

I'm attempting to use KeyListener to interact with the arrow keys, when I run it, it doesn't give me an error, but the rectangle I am trying to move left or right does not move. 我试图使用KeyListener与箭头键进行交互,但是当我运行它时,它并没有给我一个错误,但是我试图向左或向右移动的矩形不会移动。 Am I missing something? 我想念什么吗? I'm trying to simulate the game for bricks, so My only issue right now is getting the panel to move across the screen 我正在尝试模拟积木游戏,所以我现在唯一的问题是让面板在屏幕上移动

 import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;

import javax.swing.*;

import org.w3c.dom.css.Rect;

public class tools extends JPanel implements KeyListener, ActionListener {
    Timer t = new Timer(5,this);
    double x = 350, y=920,velx=0,vely=0;

    public tools() {
        t.start();
        addKeyListener(this);
        setFocusable(true);
        setFocusTraversalKeysEnabled(false);
    }


        public void paintComponent(Graphics g) {
            super.paintComponents(g);
            Graphics2D g2 = (Graphics2D)g;
            g.setColor(Color.BLUE);
            g2.fill(new Ellipse2D.Double(x, y, 300, 10));




            //ball
            /*
            g.setColor(Color.WHITE);
            g.fillOval(350, 700, 20, 20);
            */


        }




        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            x+= velx;
            y+= vely;
        }

        public void left() {
    velx = -1.5;
    vely=0;
}
        public void right() {
    velx=1.5;
    vely=0;

}

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

            if(keyCode == KeyEvent.VK_RIGHT)
            {
                right();
            }
            else if(keyCode == KeyEvent.VK_LEFT) 
            {
                left();
            }


        }

        @Override
        public void keyReleased(KeyEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void keyTyped(KeyEvent e) {
            // TODO Auto-generated method stub

        }






}

You need to call repaint() to redraw the canvas. 您需要调用repaint()重新绘制画布。 Have a look at the example below. 看下面的例子。

public class Bricks extends JPanel implements KeyListener, ActionListener {

    Timer t = new Timer(5, this);
    double x = 150, y = 320, velx = 0, vely = 0;

    public Bricks() {
        t.start();
        setFocusable(true);
        requestFocus();
        addKeyListener(this);
        //setFocusTraversalKeysEnabled(false);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponents(g);
        Graphics2D g2 = (Graphics2D) g;
        g.setColor(Color.BLUE);
        g2.fill(new Ellipse2D.Double(x, y, 300, 10));
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        x += velx;
        y += vely;
    }

    public void left() {
        velx = -1.5;
        vely = 0;
    }

    public void right() {
        velx = 1.5;
        vely = 0;
    }

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

        if (keyCode == KeyEvent.VK_RIGHT) {

            System.out.println("RIGHT");

            right();

            repaint();
        } else if (keyCode == KeyEvent.VK_LEFT) {

            System.out.println("LEFT");
            left();

            repaint();
        }

    }

    @Override
    public void keyReleased(KeyEvent e) {
        // TODO Auto-generated method stub
    }

    @Override
    public void keyTyped(KeyEvent e) {
        // TODO Auto-generated method stub
    }

    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                Bricks bricks = new Bricks();

                JFrame frame = new JFrame();
                frame.setLayout(new BorderLayout());
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
                frame.setSize(1078, 800);
                frame.getContentPane().add(bricks, BorderLayout.CENTER);

            }
        });
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM