我是一個學習Java編程,並試圖從書中編譯下面的代碼。當我嘗試編譯代碼我得到這個錯誤如何解決「擊鍵不抽象,不覆蓋ab。方法」
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));
}
}
請不要在同一時間包含多個空行的空白行。爲了人們閱讀和調試,對代碼塊使用一致的邏輯縮進。 –