Skip to content
Advertisement

Trouble spacing in JFrame

I am new to Java, what I am trying to do is generate a GUI with a bunch of images I have managed to generate a bunch of images, however, they have unnecessary space between them horizontally, is there any way that I can get rid of this currently I am generating them like so?

setLayout(new FlowLayout());
int numberOfTabs = 4;// number of times image is shown
for(int i = 0; i<numberOfTabs;i++) {
    add(
        new JLabel(new ImageIcon(getClass().getClassLoader().getResource("Path to image"))));
    //creates a new image anonymous object and adds it to the jframe        
}   
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Sheetssss");
pack();
setVisible(true);
//also in this context this = a Jframe object so all these methods can be called without an object reference

is there some other method that I can call so that the horizontal spacing can be set to 0? As shown in the image below there is space between the images, the 2 vertical black lines between images should be overlapping enter image description here Thanks

Advertisement

Answer

You just need to change

setLayout(new FlowLayout()) 

to

setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement