I have code like that:
JPanel myPanel = new JPanel();
myPanel.setLayout(new BoxLayout(myPanel, BoxLayout.Y_AXIS));
JButton button = new JButton("My Button");
JLabel label = new JLabel("My label!!!!!!!!!!!");
myPanel.add(button);
myPanel.add(label);
In this way I get elements with no distance between them. I mean, the "top" elements always touches the "bottom" element. How can I change it? I would like to have some separation between my elements?
I think about adding some "intermediate" JPanel (with some size) between my elements. But I do not think it is an elegant way to get the desired effect. Can somebody, please, help me with that?
So <spacer size="32"></spacer> will create 32px of vertical space. You can use the spacer inside of your element to provide more space or between elements.
Use the line-height property in CSS to do so. Browsers by default will create a certain amount of space between lines to ensure that the text is easily readable. For example, for 12-point type, a browser will place about 1 point of vertical space between lines.
To give to space between two divs, just add display: flex in the parent element. Then add an justify-content: space-between after adding this you will see a space between two divs.
Syntax. The border-spacing property may be specified as either one or two values. When one <length> value is specified, it defines both the horizontal and vertical spacings between cells.
JPanel myPanel = new JPanel();
myPanel.setLayout(new BoxLayout(myPanel, BoxLayout.Y_AXIS));
JButton button = new JButton("My Button");
JLabel label = new JLabel("My label!!!!!!!!!!!");
myPanel.add(button);
myPanel.add(Box.createVerticalStrut(20));
myPanel.add(label);
will be one way of doing it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With