当前位置: 代码迷 >> Java相关 >> [求助]说我这个类有错误 我看不明白阿
  详细解决方案

[求助]说我这个类有错误 我看不明白阿

热度:276   发布时间:2006-05-16 09:37:00.0
[求助]说我这个类有错误 我看不明白阿

--------------------Configuration: j2sdk1.4.2_08 <Default>--------------------
E:\LL.java:4: LL is not abstract and does not override abstract method keyTyped(java.awt.event.KeyEvent) in java.awt.event.KeyListener
public class LL extends Frame implements ActionListener,KeyListener{
^
1 error

Process completed.



这是什么错误阿


----------------解决方案--------------------------------------------------------
LL is not abstract and does not override abstract method keyTyped(java.awt.event.KeyEvent) in java.awt.event.KeyListener
LL 并不是抽象类,但是又没有重写抽象方法keyTyped(java.awt.event.KeyEvent),该方法定义在java.awt.event.KeyListener里面

这说明你声明的时候你的类是implements KeyListener的,但是你又没有实现KeyListener里面的全部的方法,所以才会出错啊
KeyListener里面有三个方法:
keyPressed(KeyEvent ke);
keyReleased(KeyEvent ke);
keyTyped(KeyEvent ke);
这三个方法你都必须要实现,否则就编译通过不了,如果你不想用这么多的话,建议你使用适配器来处理此事
KeyAdapter
----------------解决方案--------------------------------------------------------

你不是实现了KeyListener的接口吗?但是你没有没有重写他的响应方法!


----------------解决方案--------------------------------------------------------

哦~~~~ 我忘了一个方法!! 谢谢啊!!!


----------------解决方案--------------------------------------------------------

还有个问题问一下 就是这个程序没错误 就是键盘监听不管用
麻烦了 谢谢……

import java.awt.*;
import java.awt.event.*;

class LL extends Frame implements ActionListener,KeyListener{
private Button b0=new Button("0");
private Button b1=new Button("1");
private Button b2=new Button("2");
private Button b3=new Button("3");
private Button b4=new Button("4");
private Button b5=new Button("5");
private Button b6=new Button("6");
private Button b7=new Button("7");
private Button b8=new Button("8");
private Button b9=new Button("9");
private Button b=new Button(".");
private Button c1=new Button("+");
private Button c2=new Button("-");
private Button c3=new Button("*");
private Button c4=new Button("/");
private Button c5=new Button("=");
private Button bb=new Button("清除");
private TextField t1=new TextField(20);
float a=0,a1=0,a3=0,a4=0;
int a2=0;



public LL() {
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
});
Panel p1=new Panel();
p1.setLayout(new GridLayout(4,4));
p1.add(b7);p1.add(b8);p1.add(b9);p1.add(c1);
p1.add(b4);p1.add(b5);p1.add(b6);p1.add(c2);
p1.add(b1);p1.add(b2);p1.add(b3);p1.add(c3);
p1.add(b0);p1.add(c4);p1.add(c5);p1.add(b);
b0.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
c1.addActionListener(this);
c2.addActionListener(this);
c3.addActionListener(this);
c4.addActionListener(this);
c5.addActionListener(this);
b.addActionListener(this);
bb.addActionListener(this);

Panel p2=new Panel();
p2.add(t1);

Panel p3=new Panel();
p3.add(bb);

setLayout(new BorderLayout());
add("North",p2);add("Ceqnter",p1);add("South",bb);
pack();show();
}


public void keyPressed(KeyEvent ke)
{
if(ke.getKeyChar()=='0')
{
if(t1.getText().equals("0"))
{}
else
{
t1.setText(t1.getText()+("0"));
}


}
if(ke.getKeyChar()=='1')
{
if(t1.getText().equals("0"))
{
t1.setText(""+"1");
}
else
{
t1.setText(t1.getText()+"1");
}
}
if(ke.getKeyChar()=='2')
{
if(t1.getText().equals("0"))
{
t1.setText(""+"2");
}
else
{
t1.setText(t1.getText()+"2");
}
}
if(ke.getKeyChar()=='3')
{
if(t1.getText().equals("0"))
{
t1.setText(""+"3");
}
else
{
t1.setText(t1.getText()+"3");
}
}
if(ke.getKeyChar()=='4')
{
if(t1.getText().equals("0"))
{
t1.setText(""+"4");
}
else
{
t1.setText(t1.getText()+"4");
}
}
if(ke.getKeyChar()=='5')
{
if(t1.getText().equals("0"))
{
t1.setText(""+"5");
}
else
{
t1.setText(t1.getText()+"5");
}
}
if(ke.getKeyChar()=='6')
{
if(t1.getText().equals("0"))
{
t1.setText(""+"6");
}
else
{
t1.setText(t1.getText()+"6");
}
}
if(ke.getKeyChar()=='7')
{
if(t1.getText().equals("0"))
{
t1.setText(""+"7");
}
else
{
t1.setText(t1.getText()+"7");
}
}
if(ke.getKeyChar()=='8')
{
if(t1.getText().equals("0"))
{
t1.setText(""+"8");
}
else
{
t1.setText(t1.getText()+"8");
}
}
if(ke.getKeyChar()=='9')
{
if(t1.getText().equals("0"))
{
t1.setText(""+"9");
}
else
{
t1.setText(t1.getText()+"9");
}
}
if(ke.getKeyChar()=='+')
{
a1=Float.parseFloat(t1.getText());
a2=1;
t1.setText("");
}
if(ke.getKeyChar()=='-')
{
a1=Float.parseFloat(t1.getText());
a2=1;
t1.setText("");
}
if(ke.getKeyChar()=='*')
{
a1=Float.parseFloat(t1.getText());
a2=1;
t1.setText("");
}
if(ke.getKeyChar()=='/')
{
a1=Float.parseFloat(t1.getText());
a2=1;
t1.setText("");
}
if(ke.getKeyChar()=='.')
{
if(t1.getText().length()==0)
{}
else
t1.setText(t1.getText()+".");
}

}
public void keyReleased(KeyEvent ke)
{}
public void keyTyped(KeyEvent ke)
{

}





public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b0)
{
if(t1.getText().equals("0"))
{}
else
{
t1.setText(t1.getText()+("0"));
}


}
if(e.getSource()==b1)
{
if(t1.getText().equals("0"))
{
t1.setText(""+"1");
}
else
{
t1.setText(t1.getText()+"1");
}
}
if(e.getSource()==b2)
{
if(t1.getText().equals("0"))
{
t1.setText(""+"2");
}
else
{
t1.setText(t1.getText()+"2");
}
}
if(e.getSource()==b3)
{
if(t1.getText().equals("0"))
{
t1.setText(""+"3");
}
else
{
t1.setText(t1.getText()+"3");
}
}
if(e.getSource()==b4)
{
if(t1.getText().equals("0"))
{
t1.setText(""+"4");
}
else
{
t1.setText(t1.getText()+"4");
}
}
if(e.getSource()==b5)
{
if(t1.getText().equals("0"))
{
t1.setText(""+"5");
}
else
{
t1.setText(t1.getText()+"5");
}
}
if(e.getSource()==b6)
{
if(t1.getText().equals("0"))
{
t1.setText(""+"6");
}
else
{
t1.setText(t1.getText()+"6");
}
}
if(e.getSource()==b7)
{
if(t1.getText().equals("0"))
{
t1.setText(""+"7");
}
else
{
t1.setText(t1.getText()+"7");
}
}
if(e.getSource()==b8)
{
if(t1.getText().equals("0"))
{
t1.setText(""+"8");
}
else
{
t1.setText(t1.getText()+"8");
}
}
if(e.getSource()==b9)
{
if(t1.getText().equals("0"))
{
t1.setText(""+"9");
}
else
{
t1.setText(t1.getText()+"9");
}
}
if(e.getSource()==b)
{
if(t1.getText().length()==0)
{}
else
t1.setText(t1.getText()+".");
}
if(e.getSource()==bb)
{
t1.setText("");
a=0;a1=0;
a2=0;a3=0;
}
if(e.getSource()==c1)
{
a1=Float.parseFloat(t1.getText());
a2=1;
t1.setText("");
}
if(e.getSource()==c2)
{
a1=Float.parseFloat(t1.getText());
a2=2;
t1.setText("");
}
if(e.getSource()==c3)
{
a1=Float.parseFloat(t1.getText());
a2=3;
t1.setText("");
}
if(e.getSource()==c4)
{
a1=Float.parseFloat(t1.getText());
a2=4;
t1.setText("");
}
if(e.getSource()==c5)
{
switch (a2)
{
case 1: a3=a1+Float.parseFloat(t1.getText()); t1.setText(""+a3);break;
case 2: a3=a1-Float.parseFloat(t1.getText());; t1.setText(""+a3);break;
case 3: a3=a1*Float.parseFloat(t1.getText());; t1.setText(""+a3);break;
case 4: a3=a1/Float.parseFloat(t1.getText());; t1.setText(""+a3);break;
}
}

}

public static void main(String args[]) {
System.out.println("Starting AAA...");
LL mainFrame = new LL();
}
}


----------------解决方案--------------------------------------------------------
你没有注册键盘监听器啊
----------------解决方案--------------------------------------------------------
addKeyListener(this)!
----------------解决方案--------------------------------------------------------
  相关解决方案