java - 在 Java 中使用已发布的 MouseEvent 解决问题

标签 java swing mouseevent mouselistener

目前,我正在 Netbeans 中构建一个游戏,并且能够创建一个棋盘、放置棋子并允许使用 mouseveent 将它们移动到任何地方

但现在我在尝试对板上的代码进行编码以仅执行允许的操作时遇到了问题。

我遇到的问题是,每个棋子仍然允许移动,但现在当它被移动时,选定的棋子会从棋盘上完全消失,完全忽略鼠标释放事件中的所有新功能

我尝试添加的代码是针对 WhitePawn 的,它是现在唯一应该允许移动的棋子

如果尝试移动其余部分,则无论它们被拖动到哪里,都应该返回到设定的位置。我已删除与问题无关的所有代码并运行调试器。由此我知道问题出在鼠标释放事件代码中的某个地方,我只是找不到它。

    public void mouseReleased(MouseEvent e) {
        if (chessPiece == null) {
            return;
        }

        chessPiece.setVisible(false);
        Boolean success = false;
        Component c = chessBoard.findComponentAt(e.getX(), e.getY());
        String tmp = chessPiece.getIcon().toString();
        String pieceName = tmp.substring(0, (tmp.length() - 4));
        Boolean validMove = false;

//Pawn Moves
//White Pawn
        if (pieceName.equals("WhitePawn")) {
            if (startY == 1) {
                if ((startX == (e.getX() / 75)) && ((((e.getY() / 75) - startY) == 1) || ((e.getY() / 75) - startY) == 2)) {
                    if ((((e.getY() / 75) - startY) == 2)) {
                        if ((!piecePresent(e.getX(), (e.getY()))) && (!piecePresent(e.getX(), (e.getY() + 75)))) {
                            validMove = true;
                        } else {
                            validMove = false;
                        }
                    } else {
                        if ((!piecePresent(e.getX(), (e.getY())))) {
                            validMove = true;
                        } else {
                            validMove = false;
                        }
                    }
                } else {
                    validMove = false;
                }
            } else {
                int newY = e.getY() / 75;
                int newX = e.getX() / 75;
                if ((startX - 1 >= 0) || (startX + 1 <= 7)) {
                    if ((piecePresent(e.getX(), (e.getY()))) && ((((newX == (startX + 1) && (startX + 1 <= 7))) || ((newX == (startX - 1)) && (startX - 1 >= 0))))) {
                        if (checkWhiteOponent(e.getX(), e.getY())) {
                            validMove = true;
                            if (startY == 6) {
                                success = true;
                            }
                        } else {
                            validMove = false;
                        }
                    } else {
                        if (!piecePresent(e.getX(), (e.getY()))) {
                            if ((startX == (e.getX() / 75)) && ((e.getY() / 75) - startY) == 1) {
                                if (startY == 6) {
                                    success = true;
                                }
                                validMove = true;
                            } else {
                                validMove = false;
                            }
                        } else {
                            validMove = false;
                        }
                    }
                } else {
                    validMove = false;
                }
            }
        }

        if (!validMove) {
            int location = 0;
            if (startY == 0) {
                location = startX;
            } else {
                location = (startY * 8) + startX;
            }
            String pieceLocation = pieceName + ".png";
            pieces = new JLabel(new ImageIcon(getClass().getResource(pieceLocation)));
            panels = (JPanel) chessBoard.getComponent(location);
            panels.add(pieces);
        } else {
            if (success) {
                int location = 56 + (e.getX() / 75);
                if (c instanceof JLabel) {
                    Container parent = c.getParent();
                    parent.remove(0);
                    pieces = new JLabel(new ImageIcon(getClass().getResource("WhiteQueen.png")));
                    parent = (JPanel) chessBoard.getComponent(location);
                    parent.add(pieces);
                } else {
                    Container parent = (Container) c;
                    pieces = new JLabel(new ImageIcon(getClass().getResource("WhiteQueen.png")));
                    parent = (JPanel) chessBoard.getComponent(location);
                    parent.add(pieces);
                }
            } else {
                if (c instanceof JLabel) {
                    Container parent = c.getParent();
                    parent.remove(0);
                    parent.add(chessPiece);
                } else {
                    Container parent = (Container) c;
                    parent.add(chessPiece);
                }
                chessPiece.setVisible(true);
            }
        }
    }

显示“我的构建”的文件夹布局的图像,以防万一您可以看到可能未链接的地方

Folder Layout

希望有人能看到我哪里出错了,因为我只想在将 board.java 文件中的各个部分完全拆分成一个新的 java 文件之前让其中一个部分移动

最佳答案

我已经查看了您的一些代码,再次对我来说有太多内容无法完整查看,但请让我给您一些建议。

这是脆弱/危险的代码:

    JLabel awaitingPiece = (JLabel) c1;
    String tmp1 = awaitingPiece.getIcon().toString();
    if (((tmp1.contains("White")))) {

您正在使用对象的 toString() 表示作为代码逻辑的一部分,这是您永远不应该做的事情。您还对返回的字符串进行字符串操作,并再次使用返回的字符串进行代码逻辑,

    Component c = chessBoard.findComponentAt(e.getX(), e.getY());
    String tmp = chessPiece.getIcon().toString();
    String pieceName = tmp.substring(0, (tmp.length() - 4));
    Boolean validMove = false;

又是一件危险的事情。

相反,您可以获取图标并通过 equals(...) 方法比较图标是否相等。更好的方法是将逻辑从 GUI 中取出并放入程序的模型部分。如果您可以完全分离关注点,那么您将更有机会获得更小的代码单元,这些代码单元对于您和我们来说都更容易调试。

否则,为了获得更好、更完整的答案,您仍然需要首先隔离错误,为此,我仍然建议您使用 MCVE .


我还看到您正在检查图标是否名为“WhitePawn”

    chessPiece.setVisible(false);
    Boolean success = false;
    Component c = chessBoard.findComponentAt(e.getX(), e.getY());
    String tmp = chessPiece.getIcon().toString();
    String pieceName = tmp.substring(0, (tmp.length() - 4));
    Boolean validMove = false;

    //Pawn Moves
    //White Pawn
    if (pieceName.equals("WhitePawn")) {

实际上,我敢打赌它的命名方式完全不同。由于您的字符串在您的程序中发挥着关键作用(我担心这个作用太大了),您是否正在调试您的字符串值以了解代码不起作用的原因?

例如,一些 println 可以创造奇迹:

    chessPiece.setVisible(false);
    Boolean success = false;
    Component c = chessBoard.findComponentAt(e.getX(), e.getY());
    String tmp = chessPiece.getIcon().toString();
    String pieceName = tmp.substring(0, (tmp.length() - 4));

    System.out.println("pieceName is: " + pieceName); // ******* Added ********

    Boolean validMove = false;

我自己,我不会为此使用字符串,而是会使用枚举,您知道它会稳定并且完全符合您的假设。

关于java - 在 Java 中使用已发布的 MouseEvent 解决问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26893717/

相关文章:

java - 打开远程文件并写入

java - 如何设置 JFrame 的插图?

JQuery mouseup 在窗口外 – 可能吗?

wpf - 在 MVVM 中处理 MouseLeftButtonDown

java - 如何通过构建器模式强制设置参数的顺序

java - 如何获取ImageView的maxWidth和maxHeight参数?

java - 在 tomcat 服务器上安装未知的 Web 应用程序

java - 在 Fedora 中 jmyron 不支持

java - 如何在JPanel中添加一个可编辑的文本框

c++ - 按下按钮时Qt鼠标离开事件