简体   繁体   English

如何正确使用ActionListener?

[英]How to use ActionListener correctly?

I am trying to make a "simple tetras" game as a beginning coder. 我试图将一个“简单的tetras”游戏作为一个开始编码器。 As the code shown below, the white block can move by pressing arrow keys while fails to do ( y = y + 10 ) within the Timer. 如下面所示的代码,白色块可以通过按箭头键移动,而在Timer中无法执行( y = y + 10 )。 My guess is that the ActionListener is placed in the wrong position. 我的猜测是ActionListener放在错误的位置。 All I want to do is to be able to move the block left and right as it descends. 我想做的就是能够在下降时左右移动块。

Here's my code: 这是我的代码:

import java.awt.event.*;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Experiment extends JFrame {
    int x = 100;
    int y = 100;
    ASD exp = new ASD();

    public Experiment() {
        add(exp);
        exp.setFocusable(true);
    }

    public class ASD extends JPanel {
        public ASD() {
            addKeyListener(new KeyAdapter() {
                public void keyPressed(KeyEvent e) {
                    switch (e.getKeyCode()) {
                        //case KeyEvent.VK_DOWN: y += 10;break;
                        //case KeyEvent.VK_UP: y -= 10; break;
                        case KeyEvent.VK_LEFT:
                            x -= 10;
                            break;
                        case KeyEvent.VK_RIGHT:
                            x += 10;
                            break;
                    }
                    repaint();
                }
            });
        }

        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            setBackground(Color.BLACK);

            g.setColor(Color.WHITE);
            g.fillRect(x, y, 30, 30);
        }

        public class Movement extends JPanel implements ActionListener {
            Timer tm = new Timer(5, this);

            public void actionPerformed(ActionEvent e) {
                y = y + 10;
                repaint();
            }
        }
    }

    public static void main(String[] args) {
        Experiment frame = new Experiment();
        frame.setTitle("ASD");
        frame.setSize(600, 400);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

The main problems with the code were that 代码的主要问题是

  1. The timer needs to be started with the call tm.start() 需要通过调用tm.start()启动计时器
  2. After the block "falls", it needs to be repainted. 块“落”后,需要重新绘制。

Here's a working example of the class ASD 这是ASD类的一个工作示例

   public class ASD extends JPanel implements ActionListener {

        private Timer tm;

        public ASD() {
            addKeyListener(new KeyAdapter() {
                public void keyPressed(KeyEvent e) {
                    switch (e.getKeyCode()) {
                        //case KeyEvent.VK_DOWN: y += 10;break;
                        //case KeyEvent.VK_UP: y -= 10; break;
                        case KeyEvent.VK_LEFT:
                            x -= 10;
                            break;
                        case KeyEvent.VK_RIGHT:
                            x += 10;
                            break;
                    }
                    repaint();
                }
            });

            tm = new Timer(200, this);
            tm.setRepeats(true);
            tm.start();
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            y = y + 10;
            repaint();
        }

        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            setBackground(Color.BLACK);

            g.setColor(Color.WHITE);
            g.fillRect(x, y, 30, 30);
        }
    }

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

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