简体   繁体   中英

how make button work just once;

I am trying to make a game named ratsuk that is like chess but only with the knight. The knight cant move were has moved before so the 1 who cant move losses.

The problem is that when i press 1 of the buttons were the knight has appears before a new knight appears. And new 1 appears in place it shouldn't witch im not sure why.

I think it may be because the button needs to work just once.

So how do i make to button to just work once?

here is muy code

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);



    }
}

The accions for the buttons are in the acciones method. I havent program all the posible movements yet;

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;
        }
    }
}

If some 1 can give me some pointer for my project in general I will apreaciated.

Once the button has been pressed, call setEnabled(false) on it. That's it.

public void actionPerformed(ActionEvent evt) {

  // .. whatever code is needed for the button

  // and then disable the button pressed
  ((AbstractButton)evt.getSource).setEnabled(false);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM