如何正确使用ActionListener?

时间:2017-11-12 04:01:12

标签: java actionlistener

我正在尝试制作一个简单的tetras"游戏作为一个开始编码器。如下面的代码所示,白色块可以通过按箭头键移动,而在Timer中无法执行(y = y + 10)。我的猜测是ActionListener被置于错误的位置。我想做的就是能够在下降时左右移动块。

这是我的代码:

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);
    }
}

1 个答案:

答案 0 :(得分:3)

代码的主要问题是

  1. 需要通过调用tm.start()
  2. 启动计时器
  3. 块“落下”后,需要重新绘制。
  4. 以下是课程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);
            }
        }