hnlt
hnlt

Reputation: 13

Displaying GIF files in a Java GridLayout - Blank Output

I'm trying to write a program that uses Java's GridLayout to display a 2x2 set of GIFs. It runs without any errors but the output window I get is blank. The same code works to display a text label or button in a cell, so it seems that the problem is with my use of images.

All the GIF files are under the same src folder as the code, and IntelliJ is able to show them fine when hovering over the filenames -- they just don't show up when the program's run. Not sure where to check for issues.

import java.awt.*;
import javax.swing.*;

public class ShowFlags extends JFrame {
    public ShowFlags() {
        setLayout(new GridLayout(2, 2, 5, 5));

        ImageIcon imgA = new ImageIcon("us.gif");
        ImageIcon imgB = new ImageIcon("norway.gif");
        ImageIcon imgC = new ImageIcon("uk.gif");
        ImageIcon imgD = new ImageIcon("fr.gif");

        JLabel jlbA = new JLabel(imgA);
        JLabel jlbB = new JLabel(imgB);
        JLabel jlbC = new JLabel(imgC);
        JLabel jlbD = new JLabel(imgD);

        add(jlbA);
        add(jlbB);
        add(jlbC);
        add(jlbD);
    }

    public static void main(String[] args) {
        JFrame frame = new ShowFlags();
        frame.setTitle("ShowFlags");
        frame.setSize(300, 300);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

}

Upvotes: 0

Views: 50

Answers (0)

Related Questions