在jpanel上创建可点击的图像 [英] create images clickable on jpanel

查看:64
本文介绍了在jpanel上创建可点击的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何添加用户可以单击的图标(汽车,地球或其他)图像?我想用覆盖的绘制方法将它们添加到jpanel上.

How i can add icon (car, earth or other) image that can be clickabel by user? i want to add them on an jpanel with overrided paint method.

推荐答案

只需使用带有图标的JLabel.然后添加一个MouseListener来监听点击.

Just use a JLabel with an icon. Then add a MouseListener to listen for clicks.

JLabel label = new JLabel(yourIcon); // probably an ImageIcon
label.addMouseListener(new MouseAdapter(){
   public void mouseClicked(MouseEvent e) {
     System.out.println("Click at: " + e.getPoint();
   }
});

这篇关于在jpanel上创建可点击的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆