点击图像图标,获得X和Y,Java

点击图像图标,获得X和Y,Java,java,image,graphics,java-2d,mouselistener,Java,Image,Graphics,Java 2d,Mouselistener,我希望在图像中添加交互性,但没有办法在图像中添加鼠标侦听器。我想得到在图像上单击的位置的X&Y 如果图像为: tileset = new ImageIcon("xx.png"); //ImageIcon Image that wants to be clicked label.setIcon(tileset); // assigned to a label panel.add(label); //assigned to a panel tileScrollPane = new JSc

我希望在图像中添加交互性,但没有办法在图像中添加鼠标侦听器。我想得到在图像上单击的位置的X&Y

如果图像为:

tileset = new ImageIcon("xx.png"); //ImageIcon Image that wants to be clicked
label.setIcon(tileset); // assigned to a label
panel.add(label);       //assigned to a panel
tileScrollPane = new JScrollPane(panel); // Assigned to a scrollable pane
frame.add(tileScrollPane, BorderLayout.CENTER); // then onto a JFrame

您应该能够向标签添加鼠标侦听器:

label.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseClicked(MouseEvent event) {
        // Handle click - coordinates in event.
    }
});