我正在尝试练习我的GUI,但在将组件和框架之间的间隙缩小时遇到了麻烦。
上面的图片是我到目前为止所拥有的。但是我真的想在框架的左侧和“ label1”之间留一个空隙。
private void initialize() {
frame = new JFrame();
frame.setTitle("WINDOW");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 300);
panel = new JPanel();
panel.setLayout(new BorderLayout());
bottomPanel = new JPanel();
bottomPanel.setLayout(new GridLayout(1, 5));
l1 = new JLabel("Label1");
l2 = new JLabel("Label2");
l3 = new JLabel("Label3");
l4 = new JLabel("Label4");
l5 = new JLabel("Label5");
bottomPanel.add(l1);
bottomPanel.add(l2);
bottomPanel.add(l3);
bottomPanel.add(l4);
bottomPanel.add(l5);
panel.add(bottomPanel, BorderLayout.SOUTH);
frame.add(panel);
}
以上是我的代码的一部分。我试着做:bottomPanel.setLayout(new GridLayout(1、5,-20、0));留一些水平间隙,但这只会增加组件之间的间隙。根本没有将“ label1”移离框架。还有其他方法吗?我是Java的新手,所以我对其他窍门并不十分了解。我将不胜感激任何帮助!谢谢!
将Border
添加到面板:
bottomPanel = new JPanel();
bottomPanel.setBorer( new EmptyBorder(0, 10, 0, 0) 0;