2

我正在尝试制作一个名为ratsuk 的游戏,它类似于国际象棋,但仅限于骑士。不能移动的骑士之前已经移动过,所以不能移动的1损失了。

问题是,当我按下其中一个按钮时,骑士出现在新骑士出现之前。并且新的 1 出现在适当的位置,它不应该让我不知道为什么。

我认为这可能是因为按钮只需要工作一次。

那么我如何让按钮只工作一次呢?

这是muy代码

package ratsuk;

import java.util.Random;
import javax.swing.JFrame;

/**
 * import javax.swing.JFrame; import javax.swing.SwingUtilities; import
 * javax.swing.UIManager;
 */
public class Ratsuk extends JFrame {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Random rad;
        rad = new Random();
        int row = rad.nextInt(8);
        int column = rad.nextInt(8);



        Tablero newtablero = new Tablero();

        newtablero.caballo(row, column);



    }
}

按钮的 accions 在 acciones 方法中。我还没有编写所有可能的动作;

package ratsuk;

import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.Icon;
import javax.swing.ImageIcon;

/**
 *
 * @author Melvin
 */
public class Tablero {

    private static final int HEIGHT = 8;
    private static final int WIDTH = 8;
    private JButton[][] mesa;
    private Icon image;
    private JPanel panel;

    public Tablero() {
        mesa = new JButton[HEIGHT][WIDTH];
        panel = new JPanel(new GridLayout(HEIGHT, WIDTH));
        image = new ImageIcon(getClass().getResource("redKnight.gif"));

        crearventana();

        crearmesa();

        pintarmesa();
    }

    private void crearventana() {
        JFrame ventana = new JFrame("Juego de Ratsuk");
        ventana.setVisible(true);
        ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        ventana.setContentPane(panel);
        ventana.setSize(500, 500);
        ventana.setVisible(true);
    }

    private void crearmesa() {

        for (int row = 0; row < HEIGHT; row++) {
            for (int column = 0; column < WIDTH; column++) {
                JButton button = new JButton();
                button.setPreferredSize(new Dimension(40, 40));

                mesa[row][column] = button;
                panel.add(button);
            }
        }
    }

    private void pintarmesa() {
        Color fondo;
        for (int r = 0; r < HEIGHT; r++) {
            for (int t = 0; t < WIDTH; t++) {
                fondo = getBackgroundColor(r, t);

                mesa[r][t].setBackground(fondo);
            }
        }
    }

    private Color getBackgroundColor(int r, int t) {
        Color fondo;
        if (r % 2 == 0 || r == 0) {
            if (t % 2 == 0 || t == 0) {
                fondo = Color.BLACK;
            } else {
                fondo = Color.WHITE;
            }
        } else {
            if (t % 2 == 0 || t == 0) {
                fondo = Color.WHITE;
            } else {
                fondo = Color.BLACK;
            }
        }
        return fondo;
    }

    public void caballo(final int row, final int column) {

        final JButton current = mesa[row][column];

        current.setIcon(image);
        panel.repaint();

        acciones(row, column, current);
    }

    private void acciones(final int row, final int column, final JButton current) {

  final int nextRow = row + 2;
        final int nextColumn = column - 1;
        if (tienebotton(nextRow, nextColumn)==true){
            JButton next = mesa[nextRow][nextColumn];

            next.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent d) {
                    current.setIcon(null);

                    caballo(nextRow, nextColumn);
                }
            });
        }
        final int nextColumn1 = column+ 1;
        if (tienebotton(nextRow, nextColumn1)==true){
            JButton next = mesa[nextRow][nextColumn1];

            next.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent d) {
                    current.setIcon(null);

                    caballo(nextRow, nextColumn1);
                }
            });
        } 
        final int nextRow1 = row- 2;
        if (tienebotton(nextRow1, nextColumn)==true){
            JButton next = mesa[nextRow1][nextColumn];

            next.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent d) {
                    current.setIcon(null);

                    caballo(nextRow1, nextColumn);
                }
            });
        } 

    }

    private boolean tienebotton(int row, int column) {
        if (row >= 0 && row < HEIGHT && column >= 0 && column < WIDTH) {
            return true;
        } else {
            return false;
        }
    }
}

如果一些 1 可以给我一些关于我的项目的指针,我会很高兴。

4

1 回答 1

5

按下按钮后,调用setEnabled(false)它。而已。

public void actionPerformed(ActionEvent evt) {

  // .. whatever code is needed for the button

  // and then disable the button pressed
  ((AbstractButton)evt.getSource).setEnabled(false);
}
于 2013-06-16T22:24:34.133 回答