简体   繁体   English

Java Swing中面板上的KeyListener。 输出错误

[英]KeyListener on panel in java Swing. Wrong output

I want to move the white box up and down using 'w''s''a''d' keys. 我想使用'w's'a'd'键上下移动白框。 This is my code : 这是我的代码:

static int matrix[][] = { { 1, 1, 1, 1,1,1 }, { 1, 0, 0, 0,0,0 }, { 1, 3, 1, 1,0,0 }, {1,0,1,0,0,0}, {1,0,1,2,2,1}, {1,1,1,1,1,1}  };
JPanel easyPanel(){

    JPanel panel = new JPanel(new GridLayout(6, 6, 0, 0));


    int rowNumber = 0; 
    int colNumber = 0; 


    for (int i = 0; i < 36; i++) {
        if(colNumber == 6){
            colNumber = 0;
            rowNumber++; 
        }
        JLabel l = new JLabel();


        if(matrix[rowNumber][colNumber] == 0){
                l.setBackground(Color.pink);
            }
        else if(matrix[rowNumber][colNumber]==2){
            l.setIcon(new ImageIcon(new ImageIcon("cross.png").getImage().getScaledInstance(70, 70, Image.SCALE_DEFAULT)));


        }

else if(matrix[rowNumber][colNumber]==3){
                l.setIcon(new ImageIcon(new ImageIcon("icon.png").getImage().getScaledInstance(70, 70, Image.SCALE_DEFAULT)));
                int row = rowNumber;
                int col = colNumber;
                addKeyListener(new KeyListener() {

                @Override
                public void keyPressed(KeyEvent ke) {
                    //move();
                }
                @Override
                public void keyReleased(KeyEvent ke) {
                    //move();.
                }
                @Override
                public void keyTyped(KeyEvent ke) {
                    panel.setVisible(false);
                    matrix[row][col] = 0;
                    move(ke,row,col);
                }
            });
            }




   private void move(KeyEvent ke,int row,int col){
        if(ke.getKeyChar() == 'w'){
            System.out.println("Pressed up");
            matrix[row-1][col] = 3;
            JPanel newGame = easyPanel(); 
            newGame.setVisible(true);
            add(newGame); 

        }
        else if(ke.getKeyChar() =='s'){
            System.out.println("Pressed down");
            matrix[row+1][col] = 3;
            JPanel newGame = easyPanel(); 
            newGame.setVisible(true);
            add(newGame);
        }
        else if(ke.getKeyChar() == 'a'){
            System.out.println("Pressed left");
            matrix[row][col-1] = 3;
            JPanel newGame = easyPanel(); 
            newGame.setVisible(true);
            add(newGame);
        }
        else if(ke.getKeyChar() == 'd'){
            System.out.println("Pressed right");
            matrix[row][col+1] = 3;
            JPanel newGame = easyPanel(); 
            newGame.setVisible(true);
            add(newGame);
        }
        else
            System.out.println("Invalid Input");
    }

But i am getting output something like this : Default : 但是我得到这样的输出:默认: 在此处输入图片说明

First Move 'w' - correct: 先动'w'-正确: 在此处输入图片说明

Next move 'd' - insted of moving a block, it moves makes two blocks white: 下一步'd'-安装一个块,它使两个块变成白色: 在此处输入图片说明

Where am I going wrong? 我要去哪里错了? Any help will be appreciated. 任何帮助将不胜感激。

So based on this... 因此,基于此...

} else if (matrix[rowNumber][colNumber] == 3) {
    l.setIcon(new ImageIcon(new ImageIcon("icon.png").getImage().getScaledInstance(70, 70, Image.SCALE_DEFAULT)));
    int row = rowNumber;
    int col = colNumber;
    addKeyListener(new KeyListener() {

        @Override
        public void keyPressed(KeyEvent ke) {
            //move();
        }

        @Override
        public void keyReleased(KeyEvent ke) {
            //move();.
        }

        @Override
        public void keyTyped(KeyEvent ke) {
            panel.setVisible(false);
            matrix[row][col] = 0;
            move(ke, row, col);
        }
    });
}

Each time easyPanel is called and it finds a 3 in the matrix, it adds a new KeyListener , assuming you're updating the matrix correctly, this means that the first time it's called, you register one KeyListener , then the next time you call easyPanel , you add another and so and so forth. 每次调用easyPanel并在矩阵中找到3时,都会添加一个新的KeyListener ,假设您正确地更新了矩阵,这意味着第一次调用该矩阵时,您要注册一个KeyListener ,然后在下次调用easyPanel ,则添加另一个,依此类推。

The KeyListener should be registered separately and done so only once. KeyListener应该单独注册,并且只能注册一次。 In fact, I'd highly recommend using the key bindings API over KeyListener , as it will solve the focus related issues 实际上,我强烈建议您在KeyListener使用键绑定API ,因为它将解决与焦点相关的问题

I'd also consider a different update model. 我还会考虑其他更新模型。 Instead of re-creating the UI each time, simply have another matrix of JLabel s, which is first created based on the matrix values. 不必每次都重新创建UI,只需创建另一个JLabel矩阵,该matrix首先基于matrix值创建。

You can then update the matrix 然后,您可以更新matrix

matrix[row][col] = 0;

Then update the label 然后更新标签

updateUI(row, col);

which could do something like... 可以做...

switch (matrix[row][col]) {
    case 0: labels[row][col].setIcon(null);
            break;  
    case 2: labels[row][col].setIcon(crossIcon);  
            break;  
    case 3: labels[row][col].setIcon(playerIcon);  
            break;  
}
repaint();

This will reduce the amount of overhead and reduce the risk of flickering. 这将减少开销,并减少闪烁的风险。

You should also pre-cache and re-use your icons 您还应该预先缓存并重新使用图标

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

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