当前位置: 技术问答>java相关
小妹我有一个例题,不知怎么都不能编译通过,请各位帮我看看
来源: 互联网 发布时间:2015-10-16
本文导语: import java.awt.*; import java.awt.event.*; import java.util.*; public class MyCanvas implements KeyListener,MouseListener{ Canvas c; String s=""; TextField t; public static void main(String args[]){ Frame f= new Frame("Canvas"); MyCanvas mc = new MyCanv...
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class MyCanvas implements KeyListener,MouseListener{
Canvas c;
String s="";
TextField t;
public static void main(String args[]){
Frame f= new Frame("Canvas");
MyCanvas mc = new MyCanvas();
mc.c = new Canvas();
mc.t = new TextField();
f.add("South",mc.t);
f.add("Center",mc.c);
f.setSize(300,200);
mc.c.addMouseListener(mc);
mc.c.addKeyListener(mc);
f.setVisible(true);
}
public void KeyTyped(KeyEvent ev){
t.setText("KeyTyped");
s+=ev.getKeyChar();
c.getGraphics().drawString(s,0,20);
}
public void KeyPressed(KeyEvent ev){
}
public void KeyReleased(KeyEvent ev){
t.setText("Keyreleased");
}
public void mouseClicked(MouseEvent ev){
t.setText("mouseClicked");
c.requestFocus();
}
public void mousePressed(MouseEvent ev){
t.setText("mouse press");
}
public void mouseReleased(MouseEvent ev){
t.setText("mouseReleased");
}
public void mouseEntered(MouseEvent ev){
t.setText("mouseEntered");
}
public void mouseExited(MouseEvent ev){
t.setText("mouseExited");
}
}
所报的错是:D:myjavaMyCanvas.java:5: MyCanvas should be declared abstract; it does not define keyTyped(java.awt.event.KeyEvent) in MyCanvas
public class MyCanvas implements KeyListener,MouseListener{
import java.awt.event.*;
import java.util.*;
public class MyCanvas implements KeyListener,MouseListener{
Canvas c;
String s="";
TextField t;
public static void main(String args[]){
Frame f= new Frame("Canvas");
MyCanvas mc = new MyCanvas();
mc.c = new Canvas();
mc.t = new TextField();
f.add("South",mc.t);
f.add("Center",mc.c);
f.setSize(300,200);
mc.c.addMouseListener(mc);
mc.c.addKeyListener(mc);
f.setVisible(true);
}
public void KeyTyped(KeyEvent ev){
t.setText("KeyTyped");
s+=ev.getKeyChar();
c.getGraphics().drawString(s,0,20);
}
public void KeyPressed(KeyEvent ev){
}
public void KeyReleased(KeyEvent ev){
t.setText("Keyreleased");
}
public void mouseClicked(MouseEvent ev){
t.setText("mouseClicked");
c.requestFocus();
}
public void mousePressed(MouseEvent ev){
t.setText("mouse press");
}
public void mouseReleased(MouseEvent ev){
t.setText("mouseReleased");
}
public void mouseEntered(MouseEvent ev){
t.setText("mouseEntered");
}
public void mouseExited(MouseEvent ev){
t.setText("mouseExited");
}
}
所报的错是:D:myjavaMyCanvas.java:5: MyCanvas should be declared abstract; it does not define keyTyped(java.awt.event.KeyEvent) in MyCanvas
public class MyCanvas implements KeyListener,MouseListener{
|
好简单: java中 的方法一般都是 小写字母开头的,你的方法都是 大写的
当然JVM不认识啦
最好用 KeyAdapter 代替 KeyListener ,就不用写下面的了:
public void keyPressed(KeyEvent ev)
{}
当然JVM不认识啦
最好用 KeyAdapter 代替 KeyListener ,就不用写下面的了:
public void keyPressed(KeyEvent ev)
{}
|
k是小写的吧