For reference, here an example does not have a problem. It uses GridLayout(0, 1) with congruent spaces and border. Resize the frame to see the effect. Experiment with Box(BoxLayout.Y_AXIS) as an alternative.
I suspect that the source code (mis-) uses some combination of setXxxSize() or setBounds() that will display the displayed effect if the selected Look and Feel has a different geometry specified by the delegate of the button UI.

import java.awt.EventQueue; import java.awt.GridLayout; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class Buttons { private void display() { JFrame f = new JFrame("Buttons"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel p = new JPanel(new GridLayout(0, 1, 10, 10)); p.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); for (int i = 0; i < 3; i++) { p.add(new JButton("Button " + (i + 1))); } f.add(p); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true); } public static void main(String[] args) { EventQueue.invokeLater(new Buttons()::display); } }
source share