将一个新Button添加到已存在的JButtons二维数组中

时间:2013-04-16 18:25:41

标签: java swing jbutton actionlistener

所以我对编程比较陌生,我正在尝试创建一个基本的跳棋游戏。我现在遇到的麻烦是当我点击我的一个包含一块我需要的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();
        }
}
}

1 个答案:

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

所有的棋子都会由另一个类处理,而不是控制器。