java里mouselistener的问题
一个椭圆,一个长方形。鼠标可以拖动。拖动过的物体按键盘任意键可以随机变颜色。问下为什么鼠标和键盘都不好使.importacm.program.*;importacm.ut...
一个椭圆,一个长方形。鼠标可以拖动。拖动过的物体按键盘任意键可以随机变颜色。
问下为什么鼠标和键盘都不好使.
import acm.program.*;
import acm.util.RandomGenerator;
import acm.graphics.*;
import java.awt.event.*;
public class A extends GraphicsProgram{
public void init(){
GRect rect = new GRect(100,200,100,200);
rect.setFilled(true);
add(rect);
GOval oval = new GOval(10,400,100,200);
oval.setFilled(true);
add(oval);
addMouseListeners();
addKeyListeners();
}
public void MousePressed(MouseEvent e){
last = new GPoint(e.getPoint());
gobj = getElementAt(last);
}
public void MouseDragged(MouseEvent e){
if(gobj != null){
gobj.move(e.getX() - last.getX(),e.getY() - last.getY());
last = new GPoint(e.getPoint());
}
}
public void KeyPressed(KeyEvent e){
if(gobj != null){
gobj.setColor(rgen.nextColor());
}
}
private GObject gobj;
private GPoint last;
private RandomGenerator rgen = RandomGenerator.getInstance();
} 展开
问下为什么鼠标和键盘都不好使.
import acm.program.*;
import acm.util.RandomGenerator;
import acm.graphics.*;
import java.awt.event.*;
public class A extends GraphicsProgram{
public void init(){
GRect rect = new GRect(100,200,100,200);
rect.setFilled(true);
add(rect);
GOval oval = new GOval(10,400,100,200);
oval.setFilled(true);
add(oval);
addMouseListeners();
addKeyListeners();
}
public void MousePressed(MouseEvent e){
last = new GPoint(e.getPoint());
gobj = getElementAt(last);
}
public void MouseDragged(MouseEvent e){
if(gobj != null){
gobj.move(e.getX() - last.getX(),e.getY() - last.getY());
last = new GPoint(e.getPoint());
}
}
public void KeyPressed(KeyEvent e){
if(gobj != null){
gobj.setColor(rgen.nextColor());
}
}
private GObject gobj;
private GPoint last;
private RandomGenerator rgen = RandomGenerator.getInstance();
} 展开
3个回答
展开全部
1、MousePressed改为mousePressed,小写的m。
2、MouseDragged改为mouseDragged,小写的m。
3、KeyPressed改为keyPressed,小写的k。
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import acm.graphics.GObject;
import acm.graphics.GOval;
import acm.graphics.GPoint;
import acm.graphics.GRect;
import acm.program.GraphicsProgram;
import acm.util.RandomGenerator;
public class A extends GraphicsProgram {
public void init() {
GRect rect = new GRect(100, 200, 100, 200);
rect.setFilled(true);
add(rect);
GOval oval = new GOval(10, 400, 100, 200);
oval.setFilled(true);
add(oval);
addMouseListeners();
addKeyListeners();
}
public void mousePressed(MouseEvent e) {
last = new GPoint(e.getPoint());
gobj = getElementAt(last);
}
public void mouseDragged(MouseEvent e) {
if (gobj != null) {
gobj.move(e.getX() - last.getX(), e.getY() - last.getY());
last = new GPoint(e.getPoint());
}
}
public void keyPressed(KeyEvent e) {
if (gobj != null) {
gobj.setColor(rgen.nextColor());
}
}
private GObject gobj;
private GPoint last;
private RandomGenerator rgen = RandomGenerator.getInstance();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询