问下 KeyListener 在什么情况,添加事件监听,无响应。如果贴代码太麻烦了。
只求思路就行。各位大神,帮帮忙吧!

解决方案 »

  1.   

    什么组件使用 keyListener?
    像JcomboBox 这种组合的组件,使用时不能直接加载JcomboBox 实例上,没有响应的。
      

  2.   

    我做这个项目主要是用JMF 框架 做的影响 采集 和截取。 现在要给以个采集图片按钮加一个KeyListener,
    加了 但是 无响应。 我也做了个例子但是都是好使的, 所以很痛疼。
    我的示例/*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     *//*
     * OpenLab.java
     *
     * Created on 2010-3-25, 11:06:40
     */
    package test;import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import javax.swing.Timer;/**
     *
     * @author Administrator
     */
    public class OpenLab extends javax.swing.JFrame implements ActionListener {
            /** Creates new form OpenLab */
        Timer timer ;
        public OpenLab() {
            initComponents();
            timer = new Timer(1000,this);
            captureBt.setMnemonic(KeyEvent.VK_ADD);
            captureBt.addActionListener(this);
            captureBt.addKeyListener(new java.awt.event.KeyAdapter() {            public void keyPressed(java.awt.event.KeyEvent evt) {
                    jbtExitKeyPressed(evt);
                }
            });
        }    private void jbtExitKeyPressed(java.awt.event.KeyEvent evt) {
            // 用户按下组合键Ctrl + F4,调用exit()方法,退出窗体
    //        evt.getModifiers() == InputEvent.CTRL_MASK&&
            if (evt.getKeyCode() == KeyEvent.VK_ADD) {
                System.out.println("我是好人");
            }
        }    /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {        captureBt = new javax.swing.JButton();        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);        captureBt.setText("jButton1");
            captureBt.setName("captureBt"); // NOI18N
            captureBt.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    captureBtActionPerformed(evt);
                }
            });        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(113, 113, 113)
                    .addComponent(captureBt)
                    .addContainerGap(206, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(60, 60, 60)
                    .addComponent(captureBt)
                    .addContainerGap(217, Short.MAX_VALUE))
            );        pack();
        }// </editor-fold>    private void captureBtActionPerformed(java.awt.event.ActionEvent evt) {
            // TODO add your handling code here:
        }    /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {            public void run() {
                    new OpenLab().setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify
        private javax.swing.JButton captureBt;
        // End of variables declaration
        int f = 10;
        public void actionPerformed(ActionEvent e) {
            if(e.getSource() == captureBt){
                f =10;
                timer.start();        }else{
                
                f--;
                System.out.println(f);
                if (f <= 0) {
                    timer.stop();
                }
            }
        }
    }