2人跳棋游戏(Java)

时间:2015-04-09 13:17:36

标签: java eclipse

在我的春假期间,我开始写Checkers Game。我已创建了CheckersBoard,但我无法编写MouseListener部分。你能帮帮我这个部分吗?我不太确定如何实施这部分? MouseListener是否正确?或者我应该选择另一个听众?我用谷歌搜索但不知道任何线索。 非常感谢你。

public class CheckersGUI {

private final JPanel gui = new JPanel(new BorderLayout(3, 3));
private JButton[][] Squares = new JButton[8][8];
private JPanel Board;
private final JLabel message = new JLabel(
        "Ready to play!");
private static final String COLS = "ABCDEFGH";
private ImageIcon brown = new ImageIcon("brown.jpg");
private ImageIcon red = new ImageIcon("red.png");

CheckersGUI() {
    create();
    //create ButtonHandler
  /*  ButtonHandler handler = new ButtonHandler();
    for (int i=0; i<Squares.length; i++){
        for (int j=0; i<Squares[0].length; i++){
            Squares[i][j].addMouseListener(handler);
        }
    }
}

private class ButtonHandler implements MouseListener{
    public void MouseClicked ( MouseEvent event){

    }*/
}

public final void create() {
    // set up the main GUI
    gui.setBorder(new EmptyBorder(5, 5, 5, 5));
    JToolBar tools = new JToolBar();
    tools.setFloatable(false);
    gui.add(tools, BorderLayout.PAGE_START);
    Action newGameAction = new AbstractAction("New") {

        @Override
        public void actionPerformed(ActionEvent e) {
            setupNewGame();
        }
    };
    tools.add(newGameAction);
    // TODO - add functionality!

    tools.addSeparator();
    tools.add(message);

    gui.add(new JLabel(""), BorderLayout.LINE_START);

    Board = new JPanel(new GridLayout(0, 9)) ;
    Board.setBorder(new CompoundBorder(
            new EmptyBorder(8,8,8,8),
            new LineBorder(Color.BLACK)
            ));

    JPanel boardConstrain = new JPanel(new GridBagLayout());

    boardConstrain.add(Board);

    gui.add(boardConstrain);

    // create the chess board squares
    Insets buttonMargin = new Insets(0, 0, 0, 0);
    for (int ii = 0; ii < Squares.length; ii++) {
        for (int jj = 0; jj < Squares[ii].length; jj++) {
            JButton b = new JButton();
            b.setMargin(buttonMargin);

           ImageIcon icon = new ImageIcon(
                new BufferedImage(64, 64, BufferedImage.TYPE_INT_ARGB));
            b.setIcon(icon);
            if ((jj % 2 == 1 && ii % 2 == 1)                     
                 || (jj % 2 == 0 && ii % 2 == 0)) {
                b.setBackground(Color.WHITE);
            } else {
                b.setBackground(Color.BLACK);
            }
            Squares[jj][ii] = b;
        }
    }

    /*
     * fill the chess board
     */
    Board.add(new JLabel(""));
    // fill the top row
    for (int ii = 0; ii < 8; ii++) {
        Board.add(
                new JLabel(COLS.substring(ii, ii + 1),
                SwingConstants.CENTER));
    }
    // fill the black non-pawn piece row
    for (int ii = 0; ii < 8; ii++) {
        for (int jj = 0; jj < 8; jj++) {
            switch (jj) {
                case 0:
                    Board.add(new JLabel("" + (9-(ii + 1)),
                            SwingConstants.CENTER));
                default:
                    Board.add(Squares[jj][ii]);
            }
        }
    }
}

public final JComponent getGui() {
    return gui;
}

/**
 * Initializes the icons of the initial chess board piece places
 */
private final void setupNewGame() {
    message.setText("Make your move!");

    for (int ii = 0; ii < 8; ii++) {
        Squares[ii][1].setIcon(red);
    }
    for (int ii = 0; ii < 8; ii++) {
        Squares[ii][2].setIcon(red);
    }

    for (int ii = 0; ii < 8; ii++) {
        Squares[ii][5].setIcon(brown);
    }
    for (int ii = 0; ii < 8; ii++) {
        Squares[ii][6].setIcon(brown);
    }
}

public static void main(String[] args) {

            CheckersGUI cg = new CheckersGUI();
            JFrame f = new JFrame("Checkers");
            f.add(cg.getGui());
            f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            // ensures the frame is the minimum size it needs to be
            // in order display the components within it
            f.pack();
            f.setResizable(false);            
            f.setVisible(true);

        }

}

1 个答案:

答案 0 :(得分:0)

您可以为每个按钮添加ActionListener。如果按下按钮(实际上,当它被释放时)将调用actionPerformed方法。

在代码中添加以下内容,使每个方块在单击后变为红色:

public static class ButtonHandler implements ActionListener {

    private JButton b;

    public ButtonHandler(JButton b) {
        this.b = b;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        b.setBackground(Color.red);
    }

}

这是处理程序。请注意,每个处理程序实例都与一个按钮相关联。

现在,在每个按钮创建后为每个按钮创建并添加一个新的ButtonHandler

JButton b = new JButton();
...
b.addActionListener(new ButtonHandler(b));

您可以更改actionPerformed

的实施方式

示例:向getCurrentPlayer()添加方法actionPerformed,然后,如果当前玩家是玩家1,则将颜色更改为蓝色,否则,将颜色更改为红色。

我相信这会帮助您实现所需。

相关问题