Jam*_*304 2 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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
显示我的构建的文件夹布局的图像,如果我有你可以看到可能没有链接的地方

希望有人可以看到我出错的地方,因为我只想在将board.java文件完全拆分成新的java文件之前将其中的一部分移动
我已经查看了你的一些代码,再次对我来说有太多的全部内容,但请让我给你一些建议.
这是脆弱/危险的代码:
JLabel awaitingPiece = (JLabel) c1;
String tmp1 = awaitingPiece.getIcon().toString();
if (((tmp1.contains("White")))) {
Run Code Online (Sandbox Code Playgroud)
您正在使用对象的toString()表示作为代码逻辑的一部分,这是您永远不应该做的事情.您还对返回的String执行字符串操作,并再次使用返回的字符串进行代码逻辑,
Component c = chessBoard.findComponentAt(e.getX(), e.getY());
String tmp = chessPiece.getIcon().toString();
String pieceName = tmp.substring(0, (tmp.length() - 4));
Boolean validMove = false;
Run Code Online (Sandbox Code Playgroud)
再做一些危险的事情.
相反,您可以通过该equals(...)方法获取Icon并比较Icon等式.更好的方法是将您的逻辑从GUI中移出并进入程序的"模型"部分.如果您可以完全区分问题,那么您将拥有更小的代码单元,这对您和我们来说都更容易调试.
否则,为了获得更好的更完整的答案,您仍然希望首先工作以隔离错误,为此,我仍然建议您使用MCVE.
另外我看到你正在检查一个Icon是否被命名为"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")) {
Run Code Online (Sandbox Code Playgroud)
实际上,我敢打赌它的命名方式完全不同.由于你的字符串在你的程序中发挥了关键作用(我担心这个角色太大了)你是在调试你的String值来看看为什么代码不起作用?
例如,一些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;
Run Code Online (Sandbox Code Playgroud)
我自己,我不会使用字符串,而是使用枚举,你知道它会稳定,并且正是你所认为的那样.