KeyListener不会更改JPanel [英] KeyListener not changing the JPanel

查看:106
本文介绍了KeyListener不会更改JPanel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我创建了JPanel的扩展版本,但是不幸的是,它不响应任何键键入.你能告诉我问题是什么吗?我搜索了所有其他帖子,但找不到我的错误.

So i have created an extended version of JPanel and unfortunately it doesn't respond to any key typing. Could you tell me what the problem is? I searched all the others posts but i couldn't find my error.

public class MyPanel extends JPanel implements ActionListener,KeyListener{

Timer tm=new Timer(5,this);
int x=0,y=0 ,velX=0, velY=0;

public MyPanel(){
    tm.start();
    addKeyListener(this);
    setFocusable(true);
    requestFocusInWindow();
}

public void paintComponent(Graphics g){
    super.paintComponent(g);

    g.fillRect(0,0,this.getWidth(),this.getHeight());
     g.setColor(Color.RED);
    g.fillRect(x,30,50,30);



}

public void actionPerformed(ActionEvent e){

    if(x<0||x>370)
    velX=-velX;

    if(y<0||y>370)
    velY=-velY;

    y=y+velY;
    x=x+velX;
    repaint();
}

public void keyPressed(KeyEvent e){
    int c=e.getKeyCode();
    System.out.println("Cascsadas");
    if(c==KeyEvent.VK_LEFT){
        velX=-1;
        velY=0;
    }

    if(c==KeyEvent.VK_UP){
        velX=0;
        velY=-1;
    }

     if(c==KeyEvent.VK_RIGHT){
        velX=1;
        velY=0;
    }

    if(c==KeyEvent.VK_DOWN){
        velX=0;
        velY=1;
    }

}

public void keyTyped(KeyEvent e){};
public void keyReleased(KeyEvent e){};



}

我正在使用此JPanel的班级是:

And the class where i am using this JPanel is:

public class Tester
{
   public static void main(String[] args){

    MyPanel t=new MyPanel();
    JFrame jf=new JFrame();
    jf.setTitle("Tutorial");
    jf.setVisible(true);
    jf.setSize(600,400);
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jf.getContentPane().add(t,BorderLayout.CENTER);

}
}

推荐答案

您的代码不起作用,因为您的面板没有焦点.

Your code doesn't work because your panel doesn't have focus.

仅当显示在GUI上的面板可见时,requestFocusInWindow()方法才起作用.在构造函数中调用该方法无效(并且不需要).

The requestFocusInWindow() method only works when the panel is displayed on GUI is visible. Invoking the method in the constructor has no effect (and is not needed).

但是,真正的问题是在显示框架之后将面板添加到框架中.您的代码应类似于:

However, the real problem is that you add the panel to the frame AFTER the frame is visible. Your code should be something like:

jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.getContentPane().add(t,BorderLayout.CENTER);
jf.setSize(600,400);
jf.setVisible(true);

关键在于在显示框架之前将面板添加到内容窗格中.使框架可见然后更改其大小也没有意义,所以我总是使框架在最后一条语句中可见.

The key is adding the panel to the content pane before the frame is made visible. It also doesn't make sense to make the frame visible and then change its size, so I always make the frame visiable as the last statement.

我搜索了所有其他帖子,但找不到我的错误

I searched all the others posts but i couldn't find my error

我很难相信.每天都会问这个问题,我们始终建议使用Key Bindings,那么为什么还要尝试使用KeyListener?

I find that hard to believe. This question is asked daily and we always recommend to use Key Bindings, so why are you still trying to use a KeyListener?

Swing设计用于键绑定.有关更多信息和相关信息,请参见使用键盘运动.使用键绑定的方法.

Swing was designed to be used with Key Bindings. See Motion Using the Keyboard for more information and an approach that does use Key Bindings.

这篇关于KeyListener不会更改JPanel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆