错在哪儿??graphics类画线程序
importjava.awt.*;importjava.awt.event.*;classDrawLine2{Framef=newFrame("IT人资讯网");publ...
import java.awt.*;
import java.awt.event.*;
class DrawLine2{
Frame f=new Frame("IT人资讯网");
public static void main(String[] args){
new DrawLine2().init();
}
public void init(){
f.setSize(300,300);
f.setVisible(true);
f.addMouseListener(new MouseAdapter()
{
int orgX;
int orgY;
public void mousePressed(MouseEvent e){
orgX=e.getX();
orgY=e.getY();
}
public void mouseReleased(MouseEvent e){
Graphics g=f.getGraphics();
g.setColor(Color.red);
g.setFont(new Font("隶书",Font.ITALTC|Font.BOLD,30));
g.drawString(new String(orgX +","+orgY),orgX,orgY);
g.drawString(new String(orgX()+","+orgY()),e.getX(),e.getY());
g.drawLine(orgX,orgY,e.getX(),e.getY());
}
});
}
}
错误提示
--------------------Configuration: <Default>--------------------
G:\图形界面\Graphics.java:3: 类重复: DrawLine2
class DrawLine2{
^
G:\图形界面\Graphics.java:20: 无法访问 Graphics
错误的类文件: G:\图形界面\Graphics.java
文件不包含类 Graphics
请删除该文件或确保该文件位于正确的类路径子目录中。
Graphics g=f.getGraphics();
^
2 错误
呵呵,不是画线,是添加文本打印功能 展开
import java.awt.event.*;
class DrawLine2{
Frame f=new Frame("IT人资讯网");
public static void main(String[] args){
new DrawLine2().init();
}
public void init(){
f.setSize(300,300);
f.setVisible(true);
f.addMouseListener(new MouseAdapter()
{
int orgX;
int orgY;
public void mousePressed(MouseEvent e){
orgX=e.getX();
orgY=e.getY();
}
public void mouseReleased(MouseEvent e){
Graphics g=f.getGraphics();
g.setColor(Color.red);
g.setFont(new Font("隶书",Font.ITALTC|Font.BOLD,30));
g.drawString(new String(orgX +","+orgY),orgX,orgY);
g.drawString(new String(orgX()+","+orgY()),e.getX(),e.getY());
g.drawLine(orgX,orgY,e.getX(),e.getY());
}
});
}
}
错误提示
--------------------Configuration: <Default>--------------------
G:\图形界面\Graphics.java:3: 类重复: DrawLine2
class DrawLine2{
^
G:\图形界面\Graphics.java:20: 无法访问 Graphics
错误的类文件: G:\图形界面\Graphics.java
文件不包含类 Graphics
请删除该文件或确保该文件位于正确的类路径子目录中。
Graphics g=f.getGraphics();
^
2 错误
呵呵,不是画线,是添加文本打印功能 展开
展开全部
import java.awt.*;
import java.awt.event.*;
class DrawLine2{
Frame f=new Frame("IT人资讯网");
public static void main(String[] args){
new DrawLine2().init();
}
public void init(){
f.setSize(300,300);
f.setVisible(true);
f.addMouseListener(new MouseAdapter()
{
int orgX;
int orgY;
public void mousePressed(MouseEvent e){
orgX=e.getX();
orgY=e.getY();
}
public void mouseReleased(MouseEvent e){
Graphics g=f.getGraphics();
g.setColor(Color.red);
g.setFont(new Font("隶书",Font.ITALIC|Font.BOLD,30)); //是ITALIC,不是ITALTC;I和T要分清楚
g.drawString(new String(orgX +","+orgY),orgX,orgY);
g.drawString(new String(orgX+","+orgY),e.getX(),e.getY()); //orgX与orgY是你通过getX,getY得到的,而你写成orgX(),orgY()根本就不存在,当然错了,所以你直接写orgX与orgY就正确了
g.drawLine(orgX,orgY,e.getX(),e.getY());
}
});
}
}
上面的代码我运行通过,是个画线的。每通过鼠标画一条线都会相应的显示出它的坐标轴,以后多练习,就会少出错了。祝你学习进步......
import java.awt.event.*;
class DrawLine2{
Frame f=new Frame("IT人资讯网");
public static void main(String[] args){
new DrawLine2().init();
}
public void init(){
f.setSize(300,300);
f.setVisible(true);
f.addMouseListener(new MouseAdapter()
{
int orgX;
int orgY;
public void mousePressed(MouseEvent e){
orgX=e.getX();
orgY=e.getY();
}
public void mouseReleased(MouseEvent e){
Graphics g=f.getGraphics();
g.setColor(Color.red);
g.setFont(new Font("隶书",Font.ITALIC|Font.BOLD,30)); //是ITALIC,不是ITALTC;I和T要分清楚
g.drawString(new String(orgX +","+orgY),orgX,orgY);
g.drawString(new String(orgX+","+orgY),e.getX(),e.getY()); //orgX与orgY是你通过getX,getY得到的,而你写成orgX(),orgY()根本就不存在,当然错了,所以你直接写orgX与orgY就正确了
g.drawLine(orgX,orgY,e.getX(),e.getY());
}
});
}
}
上面的代码我运行通过,是个画线的。每通过鼠标画一条线都会相应的显示出它的坐标轴,以后多练习,就会少出错了。祝你学习进步......
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询