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.