如何设置垂直排列的元素之间的距离? [英] How can I set distance between elements ordered vertically?
本文介绍了如何设置垂直排列的元素之间的距离?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的代码:
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?
我想在我的元素之间添加一些中间JPanel(有一些大小)。但我不认为这是一种获得理想效果的优雅方式。请有人帮助我吗?
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?
推荐答案
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);
将是一种方式。
这篇关于如何设置垂直排列的元素之间的距离?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文