Java Swing - JPanel and GridLayout Margins / Padding

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++) {
            //Create a new ChessButton
            ChessButton button = new ChessButton("hi");
            button.setBorder(BorderFactory.createEmptyBorder());
            button.setPreferredSize(new Dimension(75, 75));
            button.setMargin(new Insets(0, 0, 0, 0));

            //Create a new JPanel that the ChessButton will go into
            JPanel buttonPanel = new JPanel();
            buttonPanel.setPreferredSize(new Dimension(75, 75));
            buttonPanel.setBorder(BorderFactory.createEmptyBorder());
            buttonPanel.add(button);

            //Add the buttonPanel to the grid
            chessBoard.add(buttonPanel);
    }

, ? Swing, , , , ! !

+3
3

, ChessButtons , , , ChessButton JPanel setPreferredSize()

. JPanel . , , . , GridLayout. , , , , . , .

, , , , (/ ?)

, . JPanel, FlowLayout, / 5 . , 10 . , .

, SSCCE, . SSCCE JButtons. . , , .

+3

; setBorderPainted(false).

: @camickr, . GridLayout .

alt text

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

/** @see http://stackoverflow.com/questions/4331699 */
public class ButtonBorder extends JPanel {

    private static final int N = 8;
    private static final int SIZE = 75;

    public ButtonBorder() {
        super(new GridLayout(N, N));
        this.setPreferredSize(new Dimension(N * SIZE, N * SIZE));
        for (int i = 0; i < N * N; i++) {
            this.add(new ChessButton(i));
        }
    }

    private static class ChessButton extends JButton {

        public ChessButton(int i) {
            super(i / N + "," + i % N);
            this.setOpaque(true);
            this.setBorderPainted(false);
            if ((i / N + i % N) % 2 == 1) {
                this.setBackground(Color.gray);
            }
        }
    }

    private void display() {
        JFrame f = new JFrame("ButtonBorder");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new ButtonBorder().display();
            }
        });
    }
}
+16

Try adding chessBoard.setPreferredSize (600, 600) to create a JPanel for a board in which there is only space for placing buttons (8 buttons in each direction * 75 size on each button).

0
source

Source: https://habr.com/ru/post/1778031/


All Articles