gpt4 book ai didi

java - 如何解决 "Keystrokes is not abstract and does not override ab. method"

转载 作者:行者123 更新时间:2023-12-01 17:31:03 24 4
gpt4 key购买 nike

我正在学习 Java 编程,正在尝试从书中编译以下代码。当我尝试编译代码时出现此错误

Keystrokes is not abstract and does not override abstract method 
keyTyped (java.awt.event.KeyEvent) in java.awt.event.KeyListener.

另外,我相信这是不相关的,但书上还告诉我将这段代码添加到源代码中(某个地方?):

pnl.add ( field) ;
pnl.add (txtArea);

当我这样做时,我得到一个标识符“预期错误”。

程序代码:

import javax.swing.*;
import java.awt.event.*;

class Keystrokes extends JFrame implements KeyListener
{
JPanel pnl = new JPanel();

public static void main (String[ ] args){
Keystrokes gui = new Keystrokes();
}

JTextField field = new JTextField ( 38 );
JTextArea txtArea = new JTextArea (5, 38);

public Keystrokes()
{
super( "Swing window");
setSize( 500,200);
setDefaultCloseOperation( EXIT_ON_CLOSE );
add(pnl);
field.addKeyListener (this );

setVisible( true );
}

public void keyPressed(KeyEvent event )
{
txtArea.setText("Key Pressed");
throw new UnsupportedOperationException("Not supported yet.");
}

public void KeyTyped (KeyEvent event ) {
throw new UnsupportedOperationException("Not supported yet.");
txtArea.append("\nCharacter : " + event.getKeyChar() );
}

public void keyReleased ( KeyEvent event)
{
int keyCode = event.getKeyCode();
txtArea.append("\nKey Code : " + event.getKeyCode() );
textArea.append("\nKey Text : " + event.getKeyText(keyCode));
}
}

最佳答案

您的方法KeyTyped应该是keyTyped。大小写很重要。

关于java - 如何解决 "Keystrokes is not abstract and does not override ab. method",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10866028/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com