为什么 KeyListener 只在没有按下按钮时才起作用?

Why does the KeyListener only work when did`t pressed the button?

我想制作一个 KeyListener 来在我按下 ESC 键时停止程序。但它只有在我什么都不做(按下按钮)时才有效。很抱歉,如果这是非常明显的事情,但我找不到错误。

package basics;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame; 


public class Graphic extends JFrame implements ActionListener, KeyListener {

 private JButton button;

    public Graphic() {
        button = new JButton();
        button.addActionListener(this);
        button.setIcon(new ImageIcon("Image.jpg"));

        this.getContentPane().add(button);
    }
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == button){
            //some code 
        }
   } 

    public static void main(String[] args) {
        JFrame bec = new Graphic();
        bec.setDefaultCloseOperation(Graphic.EXIT_ON_CLOSE);
        bec.setSize(1731, 563);
        bec.setVisible(true);
        bec.setTitle("title");
        bec.requestFocus();
        bec.addKeyListener(new Graphic());
    }

    @Override
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode() == KeyEvent.VK_ESCAPE){
            System.exit(0);
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
    }

    @Override
    public void keyTyped(KeyEvent e) {
    }

} 

您的问题是在所有组件中捕获 KeyListener。你可以这样做:

public Graphic() {
    // ...
    KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(e -> {
        if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
            dispose();
            return true;
        }
        return false;
    });
}

添加此项后,您的应用程序将在您按下 ESC 按钮后立即关闭。如果你想阻止它,例如如果你们中的一些组件有焦点(例如 JTextField 并且你想做特定的动作),那么你必须在这个监听器中关注组件并避免调用 dispose().

您确定没有 TextArea 或其他可聚焦的东西吗?他们可以获得焦点,关键事件将传递给他们而不是监听器。

UPD:抱歉,没看到你那里只有按钮。

KeyListener 存在与可聚焦性和 GUI 中的其他控件相关的问题。一个简单的解决方案是使用 Actions API。在这种方法下,程序简单地为给定组件指定任何感兴趣的键与按下(或释放)键时要调用的操作(命令)对象之间的“绑定”或“映射”。键绑定与特定的 GUI 组件相关联。

在这种情况下,正确的解决方案可能是:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.KeyStroke; 


public class Graphic extends JFrame implements ActionListener {

private JButton button;

    public Graphic() {
        getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
                KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "Cancel"); //$NON-NLS-1$
            getRootPane().getActionMap().put("Cancel", new AbstractAction(){ //$NON-NLS-1$
                public void actionPerformed(ActionEvent e)
                {
                    dispose();
                }
            });

        button = new JButton();
        button.addActionListener(this);
        button.setIcon(new ImageIcon("Image.jpg"));

        this.getContentPane().add(button);

    }
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == button){
            //some code 
        }
   } 

    public static void main(String[] args) {
        JFrame bec = new Graphic();
        bec.setDefaultCloseOperation(Graphic.EXIT_ON_CLOSE);
        bec.setSize(1731, 563);
        bec.setVisible(true);
        bec.setTitle("title");
        bec.requestFocus();
    }

} 

But it only works when I did nothing else (pressed the button).

不,它不起作用(根本)。看看这段代码:

public static void main(String[] args) {
    JFrame bec = new Graphic();
    // ..
    bec.addKeyListener(new Graphic());
}

密钥侦听器已添加到从未显示的 Graphic 的第二个实例。

它不起作用的另一个原因:因为 KeyListener(即使添加到正确的实例中)要求添加它的组件都是可聚焦的(默认情况下 JFrame 不是) 并具有输入焦点( 该帧将永远不会具有,因为它不可聚焦)。

解决方案:对于Swing,我们通常使用key bindings而不是较低级别的KeyListener。键绑定提供了指定在什么条件下调用它的方法,其中一些不需要组件具有焦点。