ew GridLayout(Max, Max); mainPanel.setLayout(grid); Chess = new JButton[Max][Max]; bEObj = new UserEvent(); //整个面板由按钮组成,按行列绘制 for (int i = 0; i < Max; i++) {
for (int j = 0; j < Max; j++) { Chess[i][j] = new JButton(); Chess[i][j].setFont(new java.awt.Font("宋体", Font.PLAIN, 15)); if (i != 0 &;&; i != Max - 1 &;&; j != 0 &;&; j != Max - 1) { Chess[i][j].setBackground(Color.green); } else { Chess[i][j].setEnabled(false); } mainPanel.add(Chess[i][j]); Chess[i][j].addActionListener(bEObj); this.setVisible(true); } } //设置外围棋盘按钮 Chess[0][0].setText("黑"); Chess[0][1].setText("0"); //Chess[0][1].setEnabled(true); Chess[0][Max - 2].setText("
白"); Chess[0][Max - 1].setText("0"); //Chess[0][Max - 1].setEnabled(true); Chess[0][Max / 2 - 2].setText("黑"); Chess[0][Max / 2 - 1].setText("方"); Chess[0][Max / 2].setText("先"); Chess[0][Max / 2 + 1].setText("下"); //设置棋盘默认初始状态 Chess[(Max - 2) / 2][(Max - 2) / 2].setText("●"); Chess[(Max - 2) / 2 + 1][(Max - 2) / 2 + 1].setText("●"); Chess[(Max - 2) / 2][(Max - 2) / 2 + 1].setText("○"); Chess[(Max - 2) / 2 + 1][(Max - 2) / 2].setText("○"); //外围颜色,去掉按钮边框,设置按钮字体颜色 for (int m = 0; m < Max; m++) { Chess[0][m].setBorder(null); Chess[Max - 1][m].setBorder(null); Chess[Max - 1][m].setForeground(Color.blue); Chess[0][m].setBackground(Color.yellow); Chess[Max - 1][m].setBackground(Color.yellow); } for (int m = 1; m < Max - 1; m++) { Chess[m][0].setBorder(null); Chess[m][Max - 1].setBorder(null);/* Chess[m][0].setText("◎"); Chess[m][Max - 1].setText("◎"); */Chess[m][0].setBackground(Color.yellow); Chess[m][Max - 1].setBackground(Color.yellow); } //功能按钮 Chess[Max - 1][1].setEnabled(true); Chess[Max - 1][2].setEnabled(true); Chess[Max - 1][3].setEnabled(true); Chess[Max - 1][4].setEnabled(true); Chess[Max - 1][1].setText("结束"); Chess[Max - 1][2].setText("新一局"); Chess[Max - 1][3].setText("让步");
Chess[Max - 1][4].setText("裁判"); //比分显示 Chess[Max - 1][6].setText("总比分"); Chess[Max - 1][7].setText("黑"+Mark1); Chess[Max - 1][8].setText("白"+Mark2); Chess[0][1].setBackground(Color.white); Chess[0][Max - 1].setBackground(Color.white); } //设置功能按钮的事件侦听事件 class UserEvent implements ActionListener { public void actionPerformed(ActionEvent e) { Object obj = e.getSource(); //System.out.println("you have pressed a area"); if (obj == Chess[Max - 1][1]) { System.exit(1); }//退出按钮 if (obj == Chess[Max - 1][2]) { new Reset(); }//退出按钮 if (obj == Chess[Max - 1][3])//让步按钮 { if (IamFirst == false)//如果白棋让步 { IamFirst = true; Chess[0][0].setBackground(Color.red); Chess[0][Max-2].setBackground(Color.white); } else //如果黑棋让步 { IamFirst = false; Chess[0][0].setBackground(Color.white); Chess[0][Max-2].setBackground(Color.red); } } if(obj == Chess[Max - 1][4])//如果按裁判按钮,计算成绩 { int M1 = 0, M2 = 0; M1 = Integer.parseInt(Chess[0][1].getText()); M2 = Integer.parseInt(Chess[0][Max - 1].getText()); if (M1>M2){ System.out.println("黑棋胜"); msg = new String("裁判郑重宣布:\n胜利方为黑棋!\n恭喜恭喜!黑棋取得胜利!继续努力吧!\n白棋不要气馁,再接再厉!"); JOptionPane.showOptionDialog(null,