문제

I am trying to create a space between an ImageIcon and a JButton, which are placed in the same JPanel side by side.

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). This will add an empty area, e. g.:

grassPanel.add(new JLabel(...));
grassPanel.add(Box.createHorizontalStrut(10));
grassPanel.add(buyGrass);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top