JPANEL面板上画图,当鼠标点上这张图时如何获得焦点?

nj_yangting 2008-02-13 02:36:18
JPANEL面板上画图,当鼠标点上这张图时如何获得焦点?
对图加载可拖动事件?
...全文
245 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
nj_yangting 2008-02-19
  • 打赏
  • 举报
回复
还有人知道吗
nj_yangting 2008-02-14
  • 打赏
  • 举报
回复
楼上这方法图可以叠加在一起吗?
365810247 2008-02-13
  • 打赏
  • 举报
回复
你可以把图片画在JLabel上,然后再添加到Jpanel上
一个图片对应一个JLabel,每个JLabel上注册事件.
JLabel响应拖动事件
chenweionline 2008-02-13
  • 打赏
  • 举报
回复
简单的方法你可以根据图像的长宽、在panel中绘制的位置和鼠标落下的位置计算出落在那张图上,你如果想实现类似“图像列表”功能,可以使用cellrenderer机制来绘制每个图像单元,参考JList的源代码。
nj_yangting 2008-02-13
  • 打赏
  • 举报
回复
public class MapPanel extends JPanel implements MouseListener,MouseMotionListener{

private static final long serialVersionUID = 1L;

private ImageIcon backimg = null;

private ImageIcon node = null;

public MapPanel(String imageName) {
this.addMouseListener(this);
backimg = new ImageIcon("D://wf.jpg");
node = new ImageIcon(imageName);
}

public void paint(Graphics g){
super.paint(g);
backimg.paintIcon(this, g, 0, 0);
node.paintIcon(this, g, 50, 50);
}

public void update(Graphics g){
super.update(g);
node.paintIcon(this, g, 50, 50);
}

public void mouseClicked(MouseEvent e) {
}

public void mouseEntered(MouseEvent e) {

}

public void mouseExited(MouseEvent e) {

}

public void mousePressed(MouseEvent e) {
addMouseMotionListener(this);
Graphics g = this.getGraphics();
node.paintIcon(this, g, e.getX()-30, e.getY()-30);
System.out.println("mousePressed");
}

public void mouseReleased(MouseEvent e) {
removeMouseMotionListener(this);
Graphics g = this.getGraphics();
node.paintIcon(this, g, e.getX()-30, e.getY()-30);
}

public void mouseDragged(MouseEvent e) {
Graphics g = this.getGraphics();
node.paintIcon(this, g, e.getX()-30, e.getY()-30);
this.repaint();

}

public void mouseMoved(MouseEvent e) {

}
}
上面就是我写的面板类
nj_yangting 2008-02-13
  • 打赏
  • 举报
回复
我意思 面板上有十个 同样的图 我点击那个是怎么知道的?
chenweionline 2008-02-13
  • 打赏
  • 举报
回复
在panel上绘图,鼠标事件可以注册在panel组件上,做图像拖动操作时,是由panel响应鼠标事件,然后计算出拖动的偏移量,根据这个偏移量重新绘图以达到图像被拖动的效果。
nj_yangting 2008-02-13
  • 打赏
  • 举报
回复
问题如果高人能解决我会加分的
chuzhijun 2008-02-13
  • 打赏
  • 举报
回复
晕死啊,这年头哪有人写那么底层的东西啊,没写过,EC的插件编写都已经带她封装好了.没写过.

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧