Java 操作侦听器编程问题?
我想做一个叫ratsuk的游戏。这就像国际象棋,但只和骑士在一起。我对奈特自动取款机的移动有问题。有些时候,当我按下一个按钮,其他骑士出现,如果它不应该(好吧,如果我不想)我很确定这是一个问题的行动侦听器,一些如何运行的方法卡巴洛不止一次。但我就是想不出来。我很绝望,请帮帮我。我对java真的很陌生 这是我需要帮助的部分Java 操作侦听器编程问题?,java,swing,Java,Swing,我想做一个叫ratsuk的游戏。这就像国际象棋,但只和骑士在一起。我对奈特自动取款机的移动有问题。有些时候,当我按下一个按钮,其他骑士出现,如果它不应该(好吧,如果我不想)我很确定这是一个问题的行动侦听器,一些如何运行的方法卡巴洛不止一次。但我就是想不出来。我很绝望,请帮帮我。我对java真的很陌生 这是我需要帮助的部分 public void caballo(final int row, final int column) { final JButton current = mesa
public void caballo(final int row, final int column) {
final JButton current = mesa[row][column];
current.setIcon(image);
panel.repaint();
acciones(row, column, current);
}
public void acciones(final int row, final int column, final JButton current) {
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
mesa[i][j].addActionListener(e(row, column, current));
}
}
}
public ActionListener e(final int row, final int column,
final JButton current) {
return new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (tienebotton(row + 2, column + 1)) {
if (e.getSource() == mesa[row + 2][column + 1]) {
current.setIcon(null);
caballo(row + 2, column + 1);
((AbstractButton) e.getSource()).setEnabled(false);
}
}
if (tienebotton(row + 2, column - 1)) {
if (e.getSource() == mesa[row + 2][column - 1]) {
current.setIcon(null);
caballo(row + 2, column - 1);
((AbstractButton) e.getSource()).setEnabled(false);
}
}
if (tienebotton(row - 2, column - 1)) {
if (e.getSource() == mesa[row - 2][column - 1]) {
current.setIcon(null);
caballo(row - 2, column - 1);
((AbstractButton) e.getSource()).setEnabled(false);
}
}
if (tienebotton(row - 2, column + 1)) {
if (e.getSource() == mesa[row - 2][column + 1]) {
current.setIcon(null);
caballo(row - 2, column + 1);
((AbstractButton) e.getSource()).setEnabled(false);
}
}
if (tienebotton(row + 1, column + 2)) {
if (e.getSource() == mesa[row + 1][column + 2]) {
current.setIcon(null);
caballo(row + 1, column + 2);
((AbstractButton) e.getSource()).setEnabled(false);
}
}
if (tienebotton(row - 1, column + 2)) {
if (e.getSource() == mesa[row - 1][column + 2]) {
current.setIcon(null);
caballo(row - 1, column + 2);
((AbstractButton) e.getSource()).setEnabled(false);
}
}
if (tienebotton(row + 1, column - 2)) {
if (e.getSource() == mesa[row + 1][column - 2]) {
current.setIcon(null);
caballo(row + 1, column - 2);
((AbstractButton) e.getSource()).setEnabled(false);
}
}
if (tienebotton(row - 1, column - 2)) {
if (e.getSource() == mesa[row - 1][column - 2]) {
current.setIcon(null);
caballo(row - 1, column - 2);
((AbstractButton) e.getSource()).setEnabled(false);
}
}
}
};
}
public void caballo(最终整数行,最终整数列){
最终接线盒电流=台面[行][列];
当前设置图标(图像);
panel.repaint();
科目(行、列、当前);
}
公共无效帐户(最终整数行、最终整数列、最终当前按钮){
对于(int i=0;i
欢迎提出任何建议
这是我放的tienebotton,所以它没有给我数组索引越界异常:
public boolean tienebotton(int row, int column) {
return (row >= 0 && row < HEIGHT && column >= 0 && column < WIDTH);
}
}
public boolean tienebotton(int行,int列){
返回(行>=0&&row=0&&column
一个改进是在之后的每一次if试验中
if (tienebotton(row+2, column+1)) {...}
改为
else如果…
这将确保只触发其中一个。欢迎使用堆栈溢出!我建议您使用调试器或
System.out.println()
调用来验证调用caballo()
的次数是否过多。学习调试代码是每个程序员的一项关键技能。tienebotton
做什么?我想逻辑可以简化。似乎在acciones
中,每次移动时,您都会在每个按钮上附加一个动作侦听器,这可能会导致当您单击某个位置时,该位置在之前的某个移动(而不是当前位置)中是有效的它在那里放置了一个骑士。有趣的是,你的代码很难阅读。。你的方法应该描述行为@MaQytieneBoton
就像hasButton
,caballo
是horse
在英语中并不意味着行为,acciones什么都不是。。就像actions(),我会用西班牙语说,你的代码是不可理解的。你一次也没有接受答案。现在您开始交叉发布:。我想我会把你的问题转给你。用这个来修正(inti=0;I