I am working on creating a chess game in Java and am currently having problems getting the GUI exactly the way I want with Swing. I use GridLayout8x8 grids to organize ChessButton(which are redefined JButtonso that I can store additional information inside them, such as coordinates). Initially, it ChessButtondidn’t appear if I hadn’t deceived them, but I solved this problem by placing each ChessButtoninside a separate one JPaneland setting each button setPreferredSize()to a given height and width.
Now my problem is that each button seems to have a small mark or pad (or / or lower?). I set setHgap(0)and setVgap(0)for GridLayout, so I'm sure that the mysterious margin comes from the buttons or JPanels. But I can't get rid of them, and they seem to make everyone ChessButtonmove a little up / down whenever I click on them.
I understand that this description of the problem can be a little difficult to visualize, so I took a screenshot (using JButton, rather than ChessButton, so the spaces are a little easier to recognize): http://img3.imageshack.us/img3/6656/jbuttonmargins.png
Here is the code I used to initialize each ChessButton:
chessBoard = new JPanel(new GridLayout(8, 8, 0, 0));
chessBoard.setBorder(BorderFactory.createEmptyBorder());
for (int i = 0; i <= 65; i++) {
ChessButton button = new ChessButton("hi");
button.setBorder(BorderFactory.createEmptyBorder());
button.setPreferredSize(new Dimension(75, 75));
button.setMargin(new Insets(0, 0, 0, 0));
JPanel buttonPanel = new JPanel();
buttonPanel.setPreferredSize(new Dimension(75, 75));
buttonPanel.setBorder(BorderFactory.createEmptyBorder());
buttonPanel.add(button);
chessBoard.add(buttonPanel);
}
, ? Swing, , , , ! !