如何使按钮只工作一次;

时间:2013-06-16 22:23:46

标签: java swing

我正在尝试制作一款名为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可以给我一些指针,我将会进行预先准备。

1 个答案:

答案 0 :(得分: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);
}