【发布时间】:2014-12-16 02:03:40
【问题描述】:
完整下载的 Github 链接
https://github.com/Jamiex304/Chess-Game
我目前正在开发一个国际象棋游戏并遇到了问题
目前,当一个棋子到达棋盘另一侧的末端时,它默认会变成一个白皇后
我想让用户决定它变成什么(因此国际象棋规则)我正在玩弄 JOption 窗格但我无法让它工作我可以用我们的错误运行它但它确实对我来说没有什么我正在寻找一些实施方面的帮助,
女王代码片段(这里没有包含完整代码,因为如果您想自己运行文件,那么在 github 链接上找到的所有文件都会很长)
if(!validMove){
int location=0;
if(startY ==0){
location = startX;
}
else{
location = (startY*8)+startX;
}
String pieceLocation = pieceName+".png";
pieces = new JLabel( new ImageIcon(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("WhiteQueen.png") );
parent = (JPanel)chessBoard.getComponent(location);
parent.add(pieces);
}
else{
Container parent = (Container)c;
pieces = new JLabel( new ImageIcon("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);
}
}
如果你想明白我的意思,请下载并运行 java 文件本身,你可以看到它只更改为白皇后的方式
【问题讨论】:
-
代码太多。继续阅读MCVE。
-
"我在想一个简单的 JOption 窗格按钮列表我只是需要一些帮助来实现"不错的选择,你需要什么帮助?你为它写了什么代码,你卡在哪里了?
-
至于
JOptionPane,您可以阅读以下内容:Multiple input in JOptionPane.showInputDialog。至于典当,我最好的猜测是你的程序认为它是白色的,而实际上它是黑色的。 -
@user1803551 我已经编辑了上面的 java 文件,仅显示典当移动和对皇后的更改而不是整个文件,我尝试添加一个 JOptionpane 列表,它正在更改为皇后片但是一直在我的代码中遇到编译错误,你能提供一些帮助吗,如果你需要源文件,它都在github上
-
您应该考虑使用常量而不是文字,而不是面向对象,而不是编写一个处理所有任务的怪物方法。您的代码可能不到一半大小。
标签: java performance swing joptionpane chess