import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class KeyBoardTest extends JFrame {
public KeyBoardTest(){
super("Try");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(200,200);
Container cp = getContentPane();
cp.setLayout(new BorderLayout());
//JPanel a = new PanelA();
JPanel b = new PanelB();
//cp.add(a,BorderLayout.NORTH);
cp.add(b,BorderLayout.CENTER);
b.setFocusable(true);
setVisible(true);
}
public static void main(String[] args) {
new KeyBoardTest();
}
}
class PanelA extends JPanel{
public PanelA(){
setLayout(new FlowLayout());
add(new JButton("PanelA"));
}
}
class PanelB extends JPanel implements KeyListener{
public PanelB(){
addKeyListener(this);
}
public void keyPressed(KeyEvent e){
System.out.println("KeyEvent");
}
public void keyReleased(KeyEvent e){
}
public void keyTyped(KeyEvent e){
}
}
这段代码,可以实现键盘监听功能!
但是,如果我把注释了的两行代码加进去,为什么就不能实现键盘监听功能了啊
哥哥姐姐,帮帮小弟。谢了