I'm using java swing. I want to add grid of pictures.
public class LayoutTest {
JFrame frame = new JFrame("GridLayout demo");
JPanel panel = new JPanel();
JButton btn1 = new JButton("First");
JButton btn2 = new JButton("Second");
JButton btn3 = new JButton("Third");
JButton btn4 = new JButton("Fourth");
JPanel panel2 = new JPanel();
JButton btn12 = new JButton("First2");
JButton btn22 = new JButton("Second2");
JButton btn32 = new JButton("Third2");
JButton btn42 = new JButton("Fourth2");
JPanel panel3 = new JPanel();
JButton btn13 = new JButton("First2");
JButton btn23 = new JButton("Second2");
JButton btn33 = new JButton("Third2");
JButton btn43 = new JButton("Fourth2");
JLabel label13 = new JLabel(new ImageIcon("pictures/building.jpg"), JLabel.CENTER);
JLabel label23 = new JLabel(new ImageIcon("pictures/building2.png"), JLabel.CENTER);
JLabel label33 = new JLabel(new ImageIcon("pictures/building.jpg"), JLabel.CENTER);
JLabel label43 = new JLabel(new ImageIcon("pictures/building2.png"), JLabel.CENTER);
public LayoutTest() {
panel3.setLayout(new GridLayout(2,2,0,0));
panel3.add(label13);
panel3.add(label23);
panel3.add(label33);
panel3.add(label43);
frame.add(panel3);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
// frame.setSize(40,40);
frame.setVisible(true);
}
}
As a result I get this: . I'm using gridLayout. My pictures are 20x20 pixels size. How can I add images without this horizontal gap?