如何在BoxLayout中的JButton之间添加垂直空间?

时间:2017-10-03 09:11:44

标签: java jpanel jbutton

我在BoxLayout中有2个按钮的面板。我想要的是在按钮之间添加垂直空间。

Sreenshot:

这是我的代码:

frame = new JFrame("FreshPos baza podataka");
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);          

JPanel panel = new JPanel();
panel.setBounds(new Rectangle(0, 5, 0, 0));
panel.setAlignmentY(Component.BOTTOM_ALIGNMENT);
frame.getContentPane().add(panel, BorderLayout.WEST);       
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

panel.setBorder( BorderFactory.createEmptyBorder(10,10,10,10) );    

JButton btnNewButton_1 = new JButton("New button");     
panel.add(btnNewButton_1);              

JButton btnNewButton_2 = new JButton("New button");                 
panel.add(btnNewButton_2);

1 个答案:

答案 0 :(得分:1)

在两个按钮之间的面板中添加一个不可见的垂直组件:

JButton btnNewButton_1 = new JButton("New button");
panel.add(btnNewButton_1);

panel.add(Box.createVerticalStrut(50));

JButton btnNewButton_2 = new JButton("New button");
panel.add(btnNewButton_2);