import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//keyevent 类的定义
class mykeylistener implements KeyListener
{
public void keyPreased(KeyEvent evt)
{
showmsg(evt);
}
public void KeyRleased(KeyEvent evt)
{
}
public void KeyTyped(KeyEvent et)
{
}
public void showmsg(KeyEvent evt
{
int code=evt.getKeyCode();
String str=evt.getKeyText(code);
char c=evt.getKeyChar();
if(code!=KeyEvent.VK_UNDEFINED)
System.out.println("the keycode is"+code);
if(c!=KeyEvent.CHAR_UNDEFINED)
System.out.println("the char is"+c);
if(evt.isShiftDown())
System.out.println("the shift is down.");
System.out.println("the key itself is "+str);
}
}
//definition of japanel
class mypanel2 extends JPanel
{
private JTextField tf;
private mykeylistener listener;
public mypanel2()
{
tf=new JTextField(20);
listener =new mykeylistener();
tf.addKeyListener(listener);
add(tf);
}
}
// definition of JFrame
class myframe2 extends JFrame
{
public myframe2()
{
mypanel2 pp2=new mypanel2();
setTitle("jjjjj");
setSize(300,300);
Container pane1=getContentPane();
pane1.add(pp2);
}
}
// the definition of main
public class a2
{
public static void main(String args[])
{
myframe2 fram2 =new myframe2();
fram2.setVisible(true);
}
}
以上的程序编译出了一个错误,:第六行mykeylistener is not abastract and dose not override abstract method keytyped..........
请大家帮帮忙,看一下究竟错在哪里了
----------------解决方案--------------------------------------------------------
你没有实现接口的方法,所以报错了!
public void keyPressed(KeyEvent evt)
{
showmsg(evt);
}
public void keyReleased(KeyEvent evt)
{
}
public void keyTyped(KeyEvent et)
{
}
上面的方法你程序都写了,可是都没有写对,方法签名要一致。下次多注意啊!
----------------解决方案--------------------------------------------------------
发现问题了是因为keyTyped 的大小写写错了,谢谢你的帮忙
----------------解决方案--------------------------------------------------------
不只是keyTyped,还有keyReleased,keyPressed你都拼写错了
----------------解决方案--------------------------------------------------------
唉,JAVA中大小写是敏感的
----------------解决方案--------------------------------------------------------
哈哈哈哈哈哈,谢谢大家
以后一定注意
----------------解决方案--------------------------------------------------------