create images clickable on jpanel

≯℡__Kan透↙ 提交于 2019-12-12 20:24:02

问题


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.


回答1:


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();
   }
});



回答2:


The easiest way is to add an Icon to a JButton, then you can use an ActionLlistener to handle the mouse click. You can also use:

button.setBorderPainted( false );

to get rid of the border so it looks like a label.



来源:https://stackoverflow.com/questions/2819884/create-images-clickable-on-jpanel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!