开发者

create images clickable on jpanel

开发者 https://www.devze.com 2022-12-30 05:23 出处:网络
How i can add icon (car, earth or other) image that can be开发者_JS百科clickabel by user? i want to add them on an jpanel with overrided paint method.Just use a JLabel with an icon. Then add a MouseLi

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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消