2个回答
展开全部
提供个思路,具体请自己去实现:
1:建立一个自定义Panel,继承自JPanel,重写它的paintComponent()方法,在方法中将图像绘制出来。
2:将该自定义Panel添加到一个组件中,并在该组件上监听mouseMoved()事件,在该事件中先获得x,y的坐标值,获取之后调用自定义组件的setLocation()方法(其它方法也可以,具体请查看API)。
1:建立一个自定义Panel,继承自JPanel,重写它的paintComponent()方法,在方法中将图像绘制出来。
2:将该自定义Panel添加到一个组件中,并在该组件上监听mouseMoved()事件,在该事件中先获得x,y的坐标值,获取之后调用自定义组件的setLocation()方法(其它方法也可以,具体请查看API)。
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
正好写了一个
import java.awt.Cursor;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JLabel;
import com.css.bztz.utils.ImageLoader;
import com.css.test.Global;
import com.css.test.cursor.MyCursor;
public class Model extends JLabel implements MouseListener,MouseMotionListener{
private Point point=new Point();
private int width=55;
private int height=55;
private int type;
public Model(int x){
//super(ImageLoader.getImageIncon("black.gif"));//
init();
this.setBounds(10+x*60, 10+x*60);
this.type=x;
}
public void init() {
this.addMouseListener(this);
this.addMouseMotionListener(this);
}
public void setBounds(int xpoint,int ypoint) {
super.setBounds(xpoint, ypoint, width, height);
}
public void setPoint(Point point){
int x=point.x-this.point.x;
int y=point.y-this.point.y;
Rectangle r=super.getBounds();
super.setBounds(r.x+x, r.y+y, r.width, r.height);
}
public void mouseClicked(MouseEvent arg0) {
System.out.println("mouseClicked");
//this.add(new);
}
/**
* 进入 lable
*/
public void mouseEntered(MouseEvent arg0) {
//System.out.println("mouseEntered");
}
/**
* 离开lable 无关
*/
public void mouseExited(MouseEvent arg0) {
//System.out.println("离开");
}
/**
* 鼠标按下
*/
public void mousePressed(MouseEvent event) {
this.point=event.getPoint();
if(this.type==99){
System.out.println("xxx");
Global.con.add(new Model(1));
Global.win.repaint();
}
}
/**
* 鼠标在他以外离开
*/
public void mouseReleased(MouseEvent event) {
event.getComponent().setCursor(Cursor.getDefaultCursor());
if(this.type==1){
//复制对象
}
}
public void mouseDragged(MouseEvent event) {
//System.out.println("mouseDragged");
//event.getComponent().setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
try{
event.getComponent().setCursor(MyCursor.getSystemCustomCursor("node_enabled.gif"));
}catch(Exception e){
}
this.setPoint(event.getPoint());
}
/**
* 鼠标移动
*/
public void mouseMoved(MouseEvent arg0) {
//System.out.println("mouseMoved");
}
}
import java.awt.Cursor;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JLabel;
import com.css.bztz.utils.ImageLoader;
import com.css.test.Global;
import com.css.test.cursor.MyCursor;
public class Model extends JLabel implements MouseListener,MouseMotionListener{
private Point point=new Point();
private int width=55;
private int height=55;
private int type;
public Model(int x){
//super(ImageLoader.getImageIncon("black.gif"));//
init();
this.setBounds(10+x*60, 10+x*60);
this.type=x;
}
public void init() {
this.addMouseListener(this);
this.addMouseMotionListener(this);
}
public void setBounds(int xpoint,int ypoint) {
super.setBounds(xpoint, ypoint, width, height);
}
public void setPoint(Point point){
int x=point.x-this.point.x;
int y=point.y-this.point.y;
Rectangle r=super.getBounds();
super.setBounds(r.x+x, r.y+y, r.width, r.height);
}
public void mouseClicked(MouseEvent arg0) {
System.out.println("mouseClicked");
//this.add(new);
}
/**
* 进入 lable
*/
public void mouseEntered(MouseEvent arg0) {
//System.out.println("mouseEntered");
}
/**
* 离开lable 无关
*/
public void mouseExited(MouseEvent arg0) {
//System.out.println("离开");
}
/**
* 鼠标按下
*/
public void mousePressed(MouseEvent event) {
this.point=event.getPoint();
if(this.type==99){
System.out.println("xxx");
Global.con.add(new Model(1));
Global.win.repaint();
}
}
/**
* 鼠标在他以外离开
*/
public void mouseReleased(MouseEvent event) {
event.getComponent().setCursor(Cursor.getDefaultCursor());
if(this.type==1){
//复制对象
}
}
public void mouseDragged(MouseEvent event) {
//System.out.println("mouseDragged");
//event.getComponent().setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
try{
event.getComponent().setCursor(MyCursor.getSystemCustomCursor("node_enabled.gif"));
}catch(Exception e){
}
this.setPoint(event.getPoint());
}
/**
* 鼠标移动
*/
public void mouseMoved(MouseEvent arg0) {
//System.out.println("mouseMoved");
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询