简体   繁体   English

如何在JPanel中的两个组件之间创建空格

[英]How can I create a space between two components in a JPanel

I am trying to create a space between an ImageIcon and a JButton , which are placed in the same JPanel side by side. 我试图在ImageIconJButton之间创建一个空格,它们并排放置在同一个JPanel

This is what I have so far: 这是我到目前为止:

allPanels.add(middlePanel);
middlePanel.add(grassPanel);
grassPanel.setLayout(new BoxLayout(grassPanel, BoxLayout.LINE_AXIS)); 
grassPanel.add(new JLabel(new ImageIcon("grass.jpg")));
buyGrass = new JButton("Buy Food");
grassPanel.add(buyGrass);

Is there anyway to separate the image and the button, so that the image is more to the left and the button is more to the right? 是否有分离图像和按钮,以便图像更左侧,按钮更右侧?

Have a look at Box.createHorizontalStrut(int) . 看看Box.createHorizontalStrut(int) This will add an empty area, eg: 这将添加一个空白区域,例如:

grassPanel.add(new JLabel(...));
grassPanel.add(Box.createHorizontalStrut(10));
grassPanel.add(buyGrass);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM