java - 为什么 KeyListener 仅在未按下按钮时才起作用?
问题描述
当我按下 ESC 键时,我想制作一个 KeyListener 来停止程序。但它只有在我什么都不做(按下按钮)时才有效。如果这是非常明显的事情,我很抱歉,但我找不到错误。
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
受到与可聚焦性和 GUI 中其他控件相关的问题的困扰。一个简单的解决方案是使用Actions API。在这种方法下,对于给定的组件,程序只需指定任何感兴趣的键与按下(或释放)该键时要调用的 Action(命令)对象之间的“绑定”或“映射”。键绑定与特定的 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();
}
}