1

所以我对编程比较陌生,我正在尝试创建一个基本的跳棋游戏。我目前遇到的问题是,当我单击一个包含一块我需要用新按钮替换它的 JButtons 时它的具体位置。到目前为止,虽然它正在删除所说的 JButton 但没有用新的 JButton 替换它,但我到目前为止所拥有的代码。

在将其应用于整个电路板之前,我一直试图仅在 0,0 块上实现这一点。

  import java.awt.*;
  import javax.swing.*;
  import java.awt.event.*;
  import java.util.*;

 public class checkersBeBitchin extends JFrame{
JButton[][] squares = new JButton[8][8];
Container box = getContentPane();
int[][] pieceTracker = new int[8][8];
JPanel board = new JPanel();

/**
 * @param args
 */
public static void main(String[] args) {
    checkersBeBitchin begin = new checkersBeBitchin();

}

public checkersBeBitchin(){
    box.setLayout(new BorderLayout());
    makeBoard();
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    setSize(600,600);
    setTitle("Checkers");
    }


@SuppressWarnings("deprecation")
private void makeBoard() {

    board.setLayout(new GridLayout(8,8));
    for (int i=0; i<8; i++){
        for (int j=0; j<8; j++) {
            squares[i][j] = new JButton();
            ActionListener actionListener = new Board();
            squares[i][j].addActionListener(actionListener);
            if((i%2 != 0 && j%2 !=0) ||(i%2==0 && j%2 == 0) ){
                    squares[i][j].setBackground(Color.black);
                    squares[i][j].setLabel("xy "+i+j);
                    //System.out.println("Black"+i+","+j); debugging
                    if(i<3){
                        int blue = 1;
                        Icon piece = new ImageIcon(getClass().getResource("/resources/piece.png"));
                        JButton button = new JButton();
                        JLabel bluePiece =new JLabel(piece);
                        pieceTracker[i][j]=blue;
                        //squares[i][j].setRolloverIcon("image dir") to make it prettier down the road.
                        squares[i][j].add(bluePiece);
                        ActionListener Listener = new Blue();
                        button.addActionListener(Listener);
                        }
                    else if (i>4){
                        int red=-1;
                        Icon piece = new ImageIcon(getClass().getResource("/resources/piece2.png"));
                        JButton button = new JButton(piece);
                        JLabel redPiece =new JLabel(piece);
                        squares[i][j].add(redPiece);
                        pieceTracker[i][j]=red;
                        ActionListener Listener = new Red();
                        button.addActionListener(Listener);
                        //squares[i][j].setRolloverSelectedIcon("/resources/piece2alt.png");
                        }

            }
            else{
                squares[i][j].setBackground(Color.white);
                squares[i][j].setLabel("xy "+i+j);
                pieceTracker[i][j]=0;
                //System.out.println("White"+i+","+j); //debugging
                }
            board.add(squares[i][j]);
            }   
        }
    box.add(board, BorderLayout.CENTER);
    }
private class Blue implements ActionListener{

        public void actionPerformed (ActionEvent e){
        System.out.println("You sexy Blue beast.");
        Object x = e.getSource();
        Component comp = (Component) x;
        board.remove(comp);
        JButton replace = new JButton();
        replace.setBackground(Color.GREEN);
        squares.toString();
        squares[5][5].add(replace);
        board.revalidate();
        board.repaint();
        System.err.println(x);

        }
    }

private class Red implements ActionListener{


    public void actionPerformed (ActionEvent e){
    //  System.out.println("You sexy Red beast.");
        Object x = e.getSource();
        Component comp = (Component) x;
        board.remove(comp);
        board.revalidate();
        board.repaint();

        }
}
private class Board implements ActionListener{


    public void actionPerformed (ActionEvent e){

        Object x = e.getSource();
        Component comp = (Component) x;
    //  System.out.println(comp);
        board.remove(comp); //removes the piece
    //  System.out.println("You clicked the board. Good for you.");
        if (e.getActionCommand().equals("xy 00")){
            System.out.println("Awesome");
    //      String s = e.getActionCommand();
            String jj = Character.toString(s.charAt(4));
            String ii = Character.toString(s.charAt(3));
    //      System.out.println("j:"+jj+" i:"+ii);
            int coordsI = Integer.parseInt(ii);
            int coordsJ = Integer.parseInt(jj);
            JButton replace = new JButton();
            replace.setBackground(Color.GREEN);
    //      squares.toString();
            squares[coordsI][coordsJ].add(replace);
            board.revalidate();
            board.repaint();
            }
        board.revalidate();
        board.repaint();
        }
}
}
4

1 回答 1

0

我不会添加/删除按钮,而是在模型中保留正方形的状态。通过这样做,您可以切换任何知道其新状态的正方形的状态。

public class MyCheckerSquare extends JButton {
    private CheckerPiece presentPiece;

    public MyCheckerSquare(Color background) {
        super();

        setBackground(background);
    }

    public void setCheckerPiece(CheckerPiece piece) {
        presentPiece = piece;
        // Change the icon regarding the given piece
    }
}

public enum CheckerPiece {
    BLACK, RED
}

所有的棋子都将由另一个类处理,有点像控制器。

于 2013-04-16T19:32:56.240 回答