Red de conocimiento informático - Material del sitio web - Cómo generar un cuadrado en Java

Cómo generar un cuadrado en Java

De esta manera, use las etiquetas una por una en el canal para dibujar y luego configure el color de las etiquetas para distinguir la apariencia de 8X8.

El siguiente es el código fuente: JPanel;

public class MyFrame extiende JFrame {

private int FIELDSIZE = 40

public MyFrame(){

setTitle("Ejemplo de una caja de Ajedrez.");

setName("test");

setBounds(400, 200, 330, 350);

setResizable(false);

JPanel boardPane = new JPanel();

boardPane.setLayout(null);

add(boardPane) ;

JLabel[][] laboratorios = nuevo JLabel[8][8]

for(int x = 0; x lt; 8; x ){

for(int y = 0; y lt; 8; y) {

JLabel backgroundLabel = new JLabel()

backgroundLabel.setOpaque(true); >

backgroundLabel.setBounds(x *FIELDSIZE, y*FIELDSIZE, FIELDSIZE, FIELDSIZE);

boardPane.add(backgroundLabel, new Integer(1), 0);

laboratorios[ x][y] = backgroundLabel;

}

}

setColor(labs);

setDefaultCloseOperation(EXIT_ON_CLOSE);

p>

setVisible(true);

}

setColor vacío privado(JLabel[][] labs){

for(int x=0; xlt; 8 ;x ){

for(int y=0; ylt; 8; y ){

si(x==0){

if(y ==0){

labs[x][y].setBackground(Color.BLACK);

}else{

if(labs[x] [y-1].getBackground().equals(Color.BLACK)){

labs[x][y].setBackground(Color.GRAY);

}else{

labs[x][y].setBackground(Color.BLACK);

}

}

}else{

if(labs[x-1][y].getBackground().equals(Color.BLACK)){

labs[x][y] .setBackground(Color.GRAY) ;

}else{

laboratorios[x][ y].setBackgroun

d(Color.NEGRO);

}

}

}

}

}

}

public static void main(String[] args) {

new MyFrame(

}

}