JTextField添加回车事件监听

如下图:txtDescription是一个文本框

            KeyStroke theEnter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0);       
            ActionMap contentActionMap = txtDescription.getActionMap();
            InputMap  contentInputMap  = txtDescription.getInputMap(this.WHEN_FOCUSED);       
            contentInputMap.put(theEnter,"Kind_of_Input");
            contentActionMap.put("Kind_of_Input",new AbstractAction(){
                public void actionPerformed(ActionEvent e){
                    System.out.println("条形码:"+txtDescription.getText());
                    resolveBarcode();
                }
            });
            txtDescription.setFocusTraversalKeysEnabled(false);

你可能感兴趣的:(swing)