swing 按钮添加了事件,测试不响应

[code="java"]
jButton1.setText("\u5f00\u59cb\u8f6c\u6362");
jButton1.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
jButton1KeyPressed(evt);
}
});
[/code]

[code="java"]
private void jButton1KeyPressed(java.awt.event.KeyEvent evt) {
// TODO add your handling code here:
}

[/code]

有知道为什么吗?谢谢。

[code="java"]
jButton1.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            System.out.println("Action");

        }
    });

[/code]

你是不是要点击按钮呀?应该用ActionListener

你是怎么进行操作测试的?你在 jButton1KeyPressed() 里加一句打印语句,然后随便敲几下键盘上的键,看看,会不会把相应的键给打印出来

[code="java"]

public static void main(String[] args) {
    JFrame f = new JFrame();
    JButton jButton1 = new JButton();
    jButton1.setText("\u5f00\u59cb\u8f6c\u6362");
    jButton1.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyPressed(java.awt.event.KeyEvent evt) {
            jButton1KeyPressed(evt);
        }
    });
    f.add(jButton1);
    f.pack();
    f.setVisible(true);
    f.setResizable(false);
    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

}

private static void jButton1KeyPressed(java.awt.event.KeyEvent evt) {
    char ch=evt.getKeyChar();
    System.out.println(ch);
}

[/code]

没有问题,会响应

[quote]key是键盘的响应?[/quote]
是的啊,所以,你看我的回复,是让你去敲键盘的啊,估计你一直是点的按钮。

addKeyListener 是响应键盘输入,你运行一下我第一次发的代码,敲键盘就能看到键盘字母打印出来了