无法在 java 中正确覆盖 keyPressed(KeyEvent)
Unable to properly override keyPressed(KeyEvent) in java
我正在尝试创建一个 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
^
在下面提供的 class 中,我确信我正确地实现了正确的关键侦听器,但显然我没有。如果有人能阐明为什么我仍然收到此错误,那就太好了!
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){}
}
您包含 keyTyped
和 keyReleased
方法,但是 KeyListener
接口要求您还包含 keyPressed
方法,即使它像其他方法一样是空白的.要解决此问题,您可以添加 public void keyPressed(KeyEvent e){}
.
考虑使用 Lambda 表达式,这对您来说超级简单。
userInput.addActionListener(e -> submitAction());
Lambda 表达式可以彻底摆脱你内心的 ActionListener class。
我不确定为什么,但 TextField 的默认操作是监听 "enter" 键。
此外,如果您想保留您的 ActionListener,您必须重写 keylistener 的其他方法,即使该操作具有空定义。
/**
* to keep compiler happy
*/
@Override
public void keyReleased(KeyEvent event) {}
@Override
public void keyTyped(KeyEvent event){}
我正在尝试创建一个 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
^
在下面提供的 class 中,我确信我正确地实现了正确的关键侦听器,但显然我没有。如果有人能阐明为什么我仍然收到此错误,那就太好了!
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){}
}
您包含 keyTyped
和 keyReleased
方法,但是 KeyListener
接口要求您还包含 keyPressed
方法,即使它像其他方法一样是空白的.要解决此问题,您可以添加 public void keyPressed(KeyEvent e){}
.
考虑使用 Lambda 表达式,这对您来说超级简单。
userInput.addActionListener(e -> submitAction());
Lambda 表达式可以彻底摆脱你内心的 ActionListener class。 我不确定为什么,但 TextField 的默认操作是监听 "enter" 键。
此外,如果您想保留您的 ActionListener,您必须重写 keylistener 的其他方法,即使该操作具有空定义。
/**
* to keep compiler happy
*/
@Override
public void keyReleased(KeyEvent event) {}
@Override
public void keyTyped(KeyEvent event){}