我有这样的代码:
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);
通过这种方式,我得到了它们之间没有距离的元素。我的意思是,“顶部”元素总是接触“底部”元素。我该如何改变它?我想在我的元素之间有一些分离?
我考虑在我的元素之间添加一些“中间”JPanel(具有一定大小)。但我认为这不是获得预期效果的优雅方式。有人可以帮我解决这个问题吗?
最佳答案
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);
将是一种方法。
关于java - 如何设置垂直排列的元素之间的距离?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2561305/