java.中我这个panel为什么不能使用键盘监听?
publicclassListenerSimpleextendsJFrame{publicListenerSimple(){JPanelpanel=newJPanel()...
public class ListenerSimple extends JFrame
{
public ListenerSimple()
{
JPanel panel = new JPanel();
add(panel);
panel.setFocusable(true);
//JOptionPane.showMessageDialog(null, "keypressed");
panel.addKeyListener(new KeyAdapter()
{
public void KeyPressed(KeyEvent e)
{
//add(new JLabel(""+e.getKeyChar()));
JOptionPane.showMessageDialog(null, "keypressed");
//System.out.println("keypressed");
}
} );
}
public static void main (String []args)
{
ListenerSimple listenersimple = new ListenerSimple();
listenersimple.setSize(200,100);
listenersimple.setTitle("ListenerSimpleTest");
listenersimple.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
listenersimple.setLocationRelativeTo(null);
listenersimple.setVisible(true);
}
} 展开
{
public ListenerSimple()
{
JPanel panel = new JPanel();
add(panel);
panel.setFocusable(true);
//JOptionPane.showMessageDialog(null, "keypressed");
panel.addKeyListener(new KeyAdapter()
{
public void KeyPressed(KeyEvent e)
{
//add(new JLabel(""+e.getKeyChar()));
JOptionPane.showMessageDialog(null, "keypressed");
//System.out.println("keypressed");
}
} );
}
public static void main (String []args)
{
ListenerSimple listenersimple = new ListenerSimple();
listenersimple.setSize(200,100);
listenersimple.setTitle("ListenerSimpleTest");
listenersimple.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
listenersimple.setLocationRelativeTo(null);
listenersimple.setVisible(true);
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询