Cómo generar un cuadrado en Java
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); p>
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{ p>
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(
}
}