gpt4 book ai didi

Java MouseClick 监听器不工作

转载 作者:行者123 更新时间:2023-11-30 04:23:35 27 4
gpt4 key购买 nike

所以我本质上正在开发一个交易或不交易的java游戏,现在我试图让图像图标在用户点击的地方消失。因此,我有一个 6 x 6 的“金属公文包”(casesArray) 图像图标数组,但无论用户单击何处,只有左上角的图像图标会消失。我猜这只是我的逻辑错误,但我似乎无法弄清楚我做错了什么......

class MouseClickListener implements MouseListener
{
public void mouseClicked (MouseEvent event)
{
if (event.getX() <= 133)row=0;
else if (event.getX() <= 267 && event.getX()>133)row=1;
else if (event.getX() <= 400 && event.getX()>267)row=2;
else if (event.getX() <= 533 && event.getX()>400)row=3;
else if (event.getX() <= 667 && event.getX()>533)row=4;
else if (event.getX() <= 800 && event.getX()>667)row=5;
if (event.getY() <= 100)column=0;
else if (event.getY() <= 200 && event.getY() > 100)column=1;
else if (event.getY() <= 300 && event.getY() > 200)column=2;
else if (event.getY() <= 400 && event.getY() > 300)column=3;
else if (event.getY() <= 500 && event.getY() > 400)column=4;
else if (event.getY() <= 600 && event.getY() > 500)column=5;

casesArray[row][column].setIcon(null);
repaint();
}
.
.
.
.
for(int i=0; i<6; i++)
{
for(int j=0; j<6; j++)
{
casesArray[i][j].addMouseListener(new MouseClickListener());
}
}

最佳答案

MouseEvent 与引发它们的组件相关。即事件的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;
}
}
}
}
}

工作示例

enter image description here

这基本上从文件夹中加载一堆图像并将它们显示在 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]);
}
}
}
}
}

关于Java MouseClick 监听器不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16409523/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com