您的位置:首页 > 其它

Jpanel下的键盘事件

2016-05-11 15:20 274 查看
Jpanel想要监听到键盘事件,必须先要获取焦点

  this.addKeyListener(new KeyAdapter(){

            @Override

            public void keyPressed(KeyEvent e)

            {

                if(e.getKeyChar()==KeyEvent.VK_1){

                    zwcpanel.setVisible(true);

                    packType = "1";

                    try {

                          musicUrl = new URL("file:"

                                + rootPath

                                + "/swing/voice/please_swipe.au");   //音乐URL

                          ac = Applet.newAudioClip(musicUrl);

                          ac.play();

                        } catch (Exception ee) {

                           ee.printStackTrace();

                        }

                }

                if(e.getKeyChar()==KeyEvent.VK_2){

//                    packButton2.doClick();

                    zwcpanel.setVisible(true);

                    packType = "2";

                    try {

                          musicUrl = new URL("file:"

                                + rootPath

                                + "/swing/voice/please_swipe.au");   //音乐URL

                          ac = Applet.newAudioClip(musicUrl);

                          ac.play();

                        } catch (Exception ee) {

                           ee.printStackTrace();

                        }

                }

                if(e.getKeyChar()==KeyEvent.VK_3){

//                    packButton3.doClick();

                    zwcpanel.setVisible(true);

                    packType = "3";

                    try {

                          musicUrl = new URL("file:"

                                + rootPath

                                + "/swing/voice/please_swipe.au");   //音乐URL

                          ac = Applet.newAudioClip(musicUrl);

                          ac.play();

                        } catch (Exception ee) {

                           ee.printStackTrace();

                        }

                }

                if(e.getKeyChar()==KeyEvent.VK_4){

//                    packButton4.doClick();

                    zwcpanel.setVisible(true);

                    packType = "4";

                    try {

                          musicUrl = new URL("file:"

                                + rootPath

                                + "/swing/voice/please_swipe.au");   //音乐URL

                          ac = Applet.newAudioClip(musicUrl);

                          ac.play();

                        } catch (Exception ee) {

                           ee.printStackTrace();

                        }

                }

            }

            });

    private static void createAndShowGUI() {

        JFrame frame = new JFrame("Frame");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setSize(400, 400);

        JPanel test = new Test();

        frame.getContentPane().add(test);

        frame.setVisible(true);

        test.requestFocus(); // 注意,与上面的这一句不能反
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Jpanel 键盘监听