代码
import javax.swing.*;
import java.awt.*;
class Chess extends JFrame {
public Chess(){
this.setTitle("国际象棋");
this.setSize(450,450);
this.setVisible(true);
int grid = 8;
int gridSize = 50;
for(int i=0;i<grid;i++){
for(int j=0;j<grid;j++){
JLabel jLabel = new JLabel();
jLabel.setSize(gridSize,gridSize);
jLabel.setLocation(i*gridSize,j*gridSize);
if( (i+j)%2==0 ){
jLabel.setBackground(Color.black);
}else{
jLabel.setBackground(Color.white);
}
jLabel.setOpaque(true);
jLabel.setBorder(BorderFactory.createLineBorder(Color.yellow));
this.add(jLabel);
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Chess();
}
});
}
}