Java swing – JPanel and GridLayout margins / padding

I'm trying to build a chess game in Java. I'm having some trouble getting the GUI exactly the way I want using swing I'm using GridLayout to organize an 8 × 8 chessbuttons grid (it covers JButton so that I can store additional information, such as coordinates) Initially, chessbuttons will not appear unless I moused over them, but I solve this problem by placing each chessbutton in a separate JPanel and setting setpreferredsize() of each button to the set height and width

Now, my question is above (and / or below?) each button There seems to be a small margin or padding I've made sure to set sethgap (0) and setvgap (0) for GridLayout, so I'm sure the mysterious edge comes from buttons or jpanels But I can't seem to get rid of them, and they seem to cause each chessbutton to move up / down a little when I'm a mouse

I realized that the description of this problem might be a little unimaginable, so I took a screenshot (using jbuttons instead of chessbuttons, so the gap is easier to identify): http://img3.imageshack.us/img3/6656/jbuttonmargins.png

This is the code I use to initialize each chessbutton:

chessBoard = new JPanel(new GridLayout(8,8,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));

            //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);
    }

So how can I get rid of these vertical spaces between buttons? I'm relatively new to swing, so if the answer is very obvious, I'm sorry, but I appreciate any help anyone may provide! Thank you in advance!

Solution

This is not the right solution There is no reason to use JPanel to press and hold the button In fact, this may be the cause of the problem When you add them to the GridLayout, the button should display If they don't display it, it may be because you added buttons to the GUI after making the GUI visible Components should be added to the GUI before they are displayed

I don't understand why there is no horizontal gap When creating a JPanel, it uses flowlayout by default, and it also contains a horizontal / vertical gap of 5 pixels So I understand why you may have a vertical gap of 10 pixels I don't understand why there is no horizontal gap

If you need more help, please show the problem on sscce Sscce should use regular JButton Before you start using custom components, learn the basics of using standard components So you know if the problem is related to your custom code

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>