package com.niit.javagui; import java.awt.BorderLayout; import java.awt.Color; import java.awt.GridBagConstraints; import java.awt.GridLayout; import java.awt.Label; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.border.Border; /** * @author: Annie * @date:2016年5月30日 * @description:国际象棋棋盘 */ public class WinGrid extends JFrame{ public static void main(String[] args) { new WinGrid(); } GridLayout grid; JPanel chessboard; public WinGrid() { grid = new GridLayout(12, 12); chessboard = new JPanel(); chessboard.setLayout(grid); Label[][] label = new Label[12][12]; for (int i = 0; i < label.length; i++) { for (int j = 0; j < label.length; j++) { label[i][j] = new Label(); if((i+j)%2==0) label[i][j].setBackground(Color.black); else label[i][j].setBackground(Color.white); chessboard.add(label[i][j]); } } add(chessboard,BorderLayout.CENTER); add(new JButton("北方参战者"),BorderLayout.NORTH); add(new JButton("南方参战者"),BorderLayout.SOUTH); add(new JButton("西方参战者"),BorderLayout.WEST); add(new JButton("东方参战者"),BorderLayout.EAST); setBounds(10, 10, 570, 390); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); validate(); } }
效果图: