this.setExtendedState(0);
this.requestFocus();
addKeyListener(new KeyAdapter() {
public void keyPressed(final KeyEvent e) {
int ch = e.getKeyCode();
System.out.println(ch+"dd");
}
});为什么不能响应事件呢?当窗口最小化后,在恢复窗口,就能响应事件了

解决方案 »

  1.   

    会不会是requestFocus()的问题?
    下面是JDK6中对requestFocus()的解释:public void requestFocus()请求此 Component 获取输入焦点,并且此 Component 的顶层祖先成为获得焦点的 Window。此 Component 对于所要许可的请求而言必须是不可显示的、可聚焦的和可见的并且其所有祖先(除了顶层 Window 以外)必须是可见的。此方法会尽力完成该请求;但是在某些情况下可能无法完成。在此 Component 接收 FOCUS_GAINED 事件前,开发人员永远不能假定此 Component 是焦点所有者。
      

  2.   

    requestFocus() 这个注释掉也一样
      

  3.   


    这不管用
    public class Key extends JFrame { private JButton button; public static void main(String args[]) {
    Key frame = new Key();
    frame.setVisible(true);
    }
    public Key() {
    super();
    getContentPane().setLayout(null);
    this.requestFocus(true);
    this.requestFocus();
    this.repaint();
    addKeyListener(new KeyAdapter() {
    public void keyPressed(final KeyEvent e) {
    System.out.println("事件发生");
    }
    });
    setBounds(100, 100, 500, 375);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    getContentPane().add(getButton());  // 这里注释掉 就能响应事件了
    } private JButton getButton() {
    if (button == null) {
    button = new JButton();
    button.setBounds(251, 164, 113, 34);
    button.setText("New JButton");
    }
    return button;
    }}
      

  4.   

    这个问题我以前遇到过 至于为什么这样我也说不清 我个人认为哦 之所以不响应是因为加了button之后 焦点就转移button上了 不在frame上了 所以才会不睬你下面两种修改方式都可以响应事件 
    第一种 索性把事件加到button上public class Key extends JFrame {    private JButton button;    public static void main(String args[]) {
            Key frame = new Key();
            frame.setVisible(true);
        }
        public Key() {
            super();
            getContentPane().setLayout(null);
            this.requestFocus(true);
            this.requestFocus();
            this.repaint();
            
            setBounds(100, 100, 500, 375);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            getContentPane().add(getButton());  // 这里注释掉 就能响应事件了
            
             getButton().addKeyListener(new KeyAdapter() {
                public void keyPressed(final KeyEvent e) {
                    System.out.println("事件发生");
                }
            });
        }    private JButton getButton() {
            if (button == null) {
                button = new JButton();
                button.setBounds(251, 164, 113, 34);
                button.setText("New JButton");
            }
            return button;
        }}
    第二种 加了button之后 在把焦点移回frame上
    public class Key extends JFrame {    private JButton button;    public static void main(String args[]) {
            Key frame = new Key();
            frame.setVisible(true);
        }
        public Key() {
            super();
            getContentPane().setLayout(null);
            this.requestFocus(true);
            this.requestFocus();
            this.repaint();
            addKeyListener(new KeyAdapter() {
                public void keyPressed(final KeyEvent e) {
                    System.out.println("事件发生");
                }
            });
            setBounds(100, 100, 500, 375);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            getContentPane().add(getButton());  // 这里注释掉 就能响应事件了
            
             this.setFocusable(true);
        }    private JButton getButton() {
            if (button == null) {
                button = new JButton();
                button.setBounds(251, 164, 113, 34);
                button.setText("New JButton");
            }
            return button;
        }}
      

  5.   

    问题解决了,JFrame 默认是不接受焦点的,设置下就OK了