不太清楚为什么我的keyListener在这里不起作用

时间:2017-05-28 09:41:18

标签: java keylistener

代码:

    public class Panel extends JPanel implements KeyListener{
    //variable for coordinates:
    private final int a1 = 155; 
    private final int a2 = (790-a1);
    private final int a5 = 47;
    private boolean gameStarted = false;
    private int rotated = 0;
    ArrayList<TetrisBlocks> blocks = new ArrayList<TetrisBlocks>();

    public Panel() //constructor
    { 
        setPreferredSize(new Dimension(1440,790)); //sets the size of the window displayed
        setBackground(Color.blue); //sets background color to blue (of the JPanel)
        setLayout(new SpringLayout()); //sets the layout of the text
        addKeyListener(this);
        setFocusable(true);

        //adds text for Game:
        add(printText("            Horizontal Tetris"));
        add(printText("                                                                  Score:"));

        //creates arrayList of Tetris Blocks:

        for (int i = 0; i <1; i++){
            blocks.add(new TetrisBlocks(1));
        }

    }

    public boolean getValue(){
        return gameStarted;
    }

    public void paintComponent(Graphics g){
        super.paintComponent(g); //overrides the paint method
        Graphics2D graphicsA = (Graphics2D) g; //creates a new Graphics 2D object

    }

    public JLabel printText(String input){
        JLabel jlabel1 = new JLabel(input); //create a new label for the text
        jlabel1.setFont(new Font("Verdana",Font.BOLD,40)); //set font type and size
        return jlabel1; //return statement for the method
    }

    public void AutomaticMove(){
        int x = 1;
        while (x <= 6){
            repaint();
            if (rotated == 1 || rotated == 3){
                blocks.get(0).MoveY();
            } else{
                blocks.get(0).MoveX(); 
            }
            x+=1;
            try { //gives computer a 1 second delay before moving the block again
                Thread.sleep(500); 
            } catch (InterruptedException e) { //throws and catches the exception when the sleep is interrupted
                e.printStackTrace();
            }
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {

        if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            System.out.println("Right key typed");
        }
        if (e.getKeyCode() == KeyEvent.VK_LEFT) {
            System.out.println("Left key typed");
        }

    }

    @Override
    public void keyPressed(KeyEvent e) {

        if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            System.out.println("Right key pressed");
        }
        if (e.getKeyCode() == KeyEvent.VK_LEFT) {
            System.out.println("Left key pressed");
        }

    }

    @Override
    public void keyReleased(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            System.out.println("Right key Released");
        }
        if (e.getKeyCode() == KeyEvent.VK_LEFT) {
            System.out.println("Left key Released");
        }
    }
}

尝试了很多不同的事情,但是关键的听众只是不回应我在键盘上的输入。任何建议或想法为什么这可能是真的?我已经在网上尝试了很多其他资源,但仍然无法弄清楚为什么关键的监听器不适用于我的程序。我的IDE或计算机没有任何问题,因为使用keylistener的其他代码工作正常。

1 个答案:

答案 0 :(得分:0)

它运行良好,但是你的Automatic Move部分代码中存在问题,当我删除了它没有问题的部分时。看图片:

enter image description here

您可以使用以下代码进行测试:

private final int a1 = 155;
private final int a2 = (790 - a1);
private final int a5 = 47;
private boolean gameStarted = false;
private int rotated = 0;
ArrayList<TetrisBlocks> blocks = new ArrayList<TetrisBlocks>();

public JTableTest() // constructor
{
    setPreferredSize(new Dimension(1440, 790)); // sets the size of the
                                                // window displayed
    setBackground(Color.blue); // sets background color to blue (of the
                                // JPanel)
    setLayout(new SpringLayout()); // sets the layout of the text
    addKeyListener(this);
    setFocusable(true);

    // adds text for Game:
    add(printText("Horizontal Tetris"));
    add(printText("Score:"));

    // creates arrayList of Tetris Blocks:

}

public boolean getValue() {
    return gameStarted;
}

public void paintComponent(Graphics g) {
    super.paintComponent(g); // overrides the paint method
    Graphics2D graphicsA = (Graphics2D) g; // creates a new Graphics 2D
                                            // object

}

public JLabel printText(String input) {
    JLabel jlabel1 = new JLabel(input); // create a new label for the text
    jlabel1.setFont(new Font("Verdana", Font.BOLD, 40)); // set font type
                                                            // and size
    return jlabel1; // return statement for the method
}



@Override
public void keyTyped(KeyEvent e) {

    if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
        System.out.println("Right key typed");
    }
    if (e.getKeyCode() == KeyEvent.VK_LEFT) {
        System.out.println("Left key typed");
    }

}

@Override
public void keyPressed(KeyEvent e) {

    if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
        System.out.println("Right key pressed");
    }
    if (e.getKeyCode() == KeyEvent.VK_LEFT) {
        System.out.println("Left key pressed");
    }

}

@Override
public void keyReleased(KeyEvent e) {
    if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
        System.out.println("Right key Released");
    }
    if (e.getKeyCode() == KeyEvent.VK_LEFT) {
        System.out.println("Left key Released");
    }
}

public static void main(String[] args) {
    JTableTest tb = new JTableTest();
    JFrame frame = new JFrame();
    frame.setPreferredSize(frame.getMaximumSize());
    frame.add(tb);
    frame.setVisible(true);

}