import java.awt.*;
import java.awt.event.*;
class Mytuxing{
private Frame f;
Mytuxing(){
tuxing();
}
private void tuxing(){
f=new Frame();
f.setBounds(300,300,300,300);
Event();
f.setVisible(true);
}
private void Event(){
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
f.addMouseListener(new MouseAdapter(){
int x;
int y;
public void mousePressed(MouseEvent m){
x=m.getX();
y=m.getY();
}
public void mouseReleased(MouseEvent v){
Graphics g=f.getGraphics();
g.setColor(Color.red);
g.drawLine(x, y, v.getX(), v.getY());
}
});
}
}
class Graphics {
public static void main(String[] args) {
// TODO Auto-generated method stub
new Mytuxing();
}
}
出现的问题是:
public void mouseReleased(MouseEvent v){
Graphics g=f.getGraphics();
g.setColor(Color.red);
g.drawLine(x, y, v.getX(), v.getY());
}这段代码
运行提示是Exception in thread "main" java.lang.Error: 无法解析的编译问题:
类型不匹配:不能从 Graphics 转换为 Graphics
没有为类型 Graphics 定义方法 setColor(Color)
没有为类型 Graphics 定义方法 drawLine(int, int, int, int)
at Mytuxing.Event(Graphics.java:39)
at Mytuxing.tuxing(Graphics.java:13)
at Mytuxing.<init>(Graphics.java:8)
at Graphics.main(Graphics.java:57)
------解决方案--------------------
不要自己定义:
class Graphics {
跟java.awt.Graphics 重名了,结果把Java给整糊涂了