【问题标题】:Unable to properly override keyPressed(KeyEvent) in java无法在 java 中正确覆盖 keyPressed(KeyEvent)
【发布时间】:2017-04-06 22:37:30
【问题描述】:

我正在尝试创建一个 keylistener 来检测用户何时点击“enter”键,但每次编译时都会收到错误消息:

 NameBox.java:6: error: NameBox is not abstract and does not override 
 abstract method keyPressed(KeyEvent) in KeyListener
 public class NameBox extends JFrame implements KeyListener
        ^

在下面提供的课程中,我确信我正确地实现了正确的关键监听器,但显然我没有。如果有人能解释为什么我仍然会收到这个错误,那就太棒了!

import java.util.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class NameBox extends JFrame implements KeyListener
{
    String userWord = "";
    JTextField userInput = new JTextField(20);
    JButton submit = new JButton("Submit");

    public NameBox()
    {
        super("Enter your name");
        JPanel centerPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 15, 15));
        setSize(400, 200);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        submit.addKeyListener(new KeyAdapter(){
            public void keyPressed(KeyEvent evt)
            {
                if(evt.getKeyCode() == KeyEvent.VK_ENTER)
                {
                    submitAction();
                }
            }
        });
        centerPanel.add(userInput);
        JPanel southPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 15, 15));
        southPanel.add(submit);
        Box theBox = Box.createVerticalBox();
        theBox.add(Box.createVerticalStrut(100));
        theBox.add(centerPanel);
        theBox.add(Box.createVerticalStrut(200));
        theBox.add(southPanel);
        add(theBox);

    }
    private void submitAction()
    {
        userWord = userInput.getText();
    }
    public static void main(String[] args)
    {
        new NameBox().setVisible(true);
    }
    @Override
    public void keyTyped(KeyEvent e){}
    @Override
    public void keyReleased(KeyEvent e){}

}

【问题讨论】:

  • 错误不言自明,您已承诺实现KeyListener 接口指定的合约,但未能提供实现 - 相反,出于某种奇怪的原因,您添加了一个KeyListener 到按钮,而不是使用为其设计的ActionListener
  • 我强烈建议你看看How to use buttonsHow to write an action listener,如果出于某种奇怪的原因,你仍然需要监控关键事件,How to Use Key Bindings 因为KeyListener 只是一个在...代码中绘制

标签: java swing keylistener


【解决方案1】:

您包括keyTypedkeyReleased 方法,但KeyListener 接口要求您还包括keyPressed 方法,即使它像其他方法一样是空白的。要解决此问题,您可以添加 public void keyPressed(KeyEvent e){}

【讨论】:

  • 非常感谢,我不知道即使您在课堂上使用 keyPressed 也必须覆盖它!
【解决方案2】:

考虑使用 Lambda 表达式,在您的情况下它超级简单。

userInput.addActionListener(e -> submitAction());

Lambda 表达式可以完全摆脱您的内部 ActionListener 类。 我不知道为什么,但 TextField 的默认操作是侦听“输入”键。

此外,如果您想保留 ActionListener,您必须覆盖 keylistener 的其他方法,即使该操作的定义为空。

        /**
         * to keep compiler happy
         */
        @Override
        public void keyReleased(KeyEvent event) {}
        @Override
        public void keyTyped(KeyEvent event){}

【讨论】:

    猜你喜欢
    • 2014-04-16
    • 2012-05-30
    • 2015-05-06
    • 1970-01-01
    • 2016-10-19
    • 2015-10-16
    • 2014-08-24
    • 1970-01-01
    相关资源
    最近更新 更多