开发者

create images clickable on jpanel

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜