MouseEvent
s 与引发它们的组件相关。也就是说,事件的 x/y 点会自动转换为发生事件的组件的坐标空间。
因此,如果一个组件存在于其父级中的 100x100 处,当您单击它时,鼠标事件将被转换为坐标为 0x0,表示被单击组件的顶层角。
尝试直接直接引用组件,而不是使用一系列if
语句...
class MouseClickListener implements MouseListener
{
public void mouseClicked (MouseEvent event)
{
Component comp = event.getComponent();
if (comp instanceof JLabel) {
((JLabel)comp).setIcon(null);
}
}
}
更新了其他示例
MouseEvent#getSource
返回对创建事件的对象的引用...
class MouseClickListener implements MouseListener
{
public void mouseClicked (MouseEvent event)
{
Object source = event.getSource();
for (int x = 0; x < casesArray.length; x++) {
for (int y = 0; y < casesArray[x].length; y++) {
if (casesArray[x][y] == source) {
casesArray[x][y].setIcon(null);
break;
}
}
}
}
}
一个更安全的解决方案是继续getComponent
用作事件源,这可能与事件发生的组件不同......
class MouseClickListener implements MouseListener
{
public void mouseClicked (MouseEvent event)
{
Component source = event.getComponent();
for (int x = 0; x < casesArray.length; x++) {
for (int y = 0; y < casesArray[x].length; y++) {
if (casesArray[x][y] == source) {
casesArray[x][y].setIcon(null);
break;
}
}
}
}
}
工作示例
这基本上从文件夹中加载一堆图像并将它们显示在 6x6 网格中。单击任何图像将其相应标签的图标设置为null
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class ClickTest {
public static void main(String[] args) {
new ClickTest();
}
public ClickTest() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JLabel[][] casesArray;
public TestPane() {
setLayout(new GridLayout(0, 6));
File[] files = new File("/path/to/lots/of/images").listFiles();
MouseListener listener = new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent event) {
Object source = event.getSource();
for (int x = 0; x < casesArray.length; x++) {
for (int y = 0; y < casesArray[x].length; y++) {
if (casesArray[x][y] == source) {
casesArray[x][y].setIcon(null);
break;
}
}
}
}
};
casesArray = new JLabel[6][6];
int index = 0;
for (int x = 0; x < 6; x++) {
for (int y = 0; y < 6; y++) {
JLabel label = new JLabel();
try {
label.setIcon(new ImageIcon(ImageIO.read(files[index++])));
} catch (IOException ex) {
ex.printStackTrace();
}
label.addMouseListener(listener);
casesArray[x][y] = label;
add(casesArray[x][y]);
}
}
}
}
}