我是一名学习 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));
}
}