Galera eu estou tentando usar o GridBagLayout mas estou com um probelma, as celulas nao tem um numero fixo, por exemplo a celula (0,0) é na verdade (0,2) estou mandando o código pra esclarecer minha duvida
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class InterfaceGrafica extends JFrame {
protected JPanel painelPrincipal;
protected Vector<JPanel> casas;
protected Color cor;
protected GridBagConstraints cons, ultimoClick;
public InterfaceGrafica() {
casas = new Vector<JPanel>();
painelPrincipal = new JPanel();
painelPrincipal.setLayout(new GridBagLayout());
cons = new GridBagConstraints();
ultimoClick = new GridBagConstraints();
cons.weightx = 1;
cons.weighty = 1;
cons.fill = GridBagConstraints.BOTH;
for (int i = 0; i < 64; i++) {
casas.add(new JPanel());
casas.get(i).addMouseListener(new MouseListener() {
public void mouseEntered(MouseEvent e) {
cor = e.getComponent().getBackground();
e.getComponent().setBackground(Color.YELLOW);
}
public void mouseExited(MouseEvent e) {
e.getComponent().setBackground(cor);
}
public void mouseClicked(MouseEvent e) {
ultimoClick.gridx = e.getComponent().getX();
ultimoClick.gridy = e.getComponent().getY();
System.out.println("x: " + ultimoClick.gridx + "\ny: "
+ ultimoClick.gridy);
}
public void mousePressed(MouseEvent arg0) {
}
public void mouseReleased(MouseEvent arg0) {
}
});
if (((((i < 8) | ((i > 16) & (i < 24)) | ((i > 32) & (i < 40)) | ((i > 48) & (i < 56))) & (i % 2 == 1)))
| (((((i > 7) & (i < 15)) | ((i > 23) & (i < 31))
| ((i > 39) & (i < 47)) | (i > 55)) & (i % 2 == 0)))) {
casas.get(i).setBackground(Color.BLACK);
} else {
casas.get(i).setBackground(Color.WHITE);
}
}
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
cons.gridx = j;
cons.gridy = i;
painelPrincipal.add(casas.get(i * 8 + j), cons);
}
}
add(painelPrincipal);
setSize(400, 400);
setLocation(300, 150);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
InterfaceGrafica jogo = new InterfaceGrafica();
}
}