最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Image is not being rendered in swing frame - Stack Overflow

programmeradmin4浏览0评论

I have made sure that image path is correct and syntaxes are correct for adding a label to a frame.

public class Main {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        JLabel label = new JLabel();
        label.setText("Hello");
        
        ImageIcon nintendoImage = new ImageIcon("Images/nintendo.jpg");
        label.setIcon(nintendoImage);
        frame.add(label);
        frame.setSize(400, 400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

I have also tried to use Absolute path for the image(copying from Finder(Mac)), even tried putting the img in same package. And, yes I have all the necessary imports), but it didn't work

This image is of my project directory. I am running it from Vs Code, tried also from IntelliJ but it didn't help

I have made sure that image path is correct and syntaxes are correct for adding a label to a frame.

public class Main {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        JLabel label = new JLabel();
        label.setText("Hello");
        
        ImageIcon nintendoImage = new ImageIcon("Images/nintendo.jpg");
        label.setIcon(nintendoImage);
        frame.add(label);
        frame.setSize(400, 400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

I have also tried to use Absolute path for the image(copying from Finder(Mac)), even tried putting the img in same package. And, yes I have all the necessary imports), but it didn't work

This image is of my project directory. I am running it from Vs Code, tried also from IntelliJ but it didn't help

Share Improve this question edited Jan 20 at 4:12 Corvus-OS asked Jan 19 at 1:24 Corvus-OSCorvus-OS 11 bronze badge 10
  • 1 Use ImageIO to read images. ImageIO throws an exception if the image path is incorrect. Search "java resources folder" for the best way to package images in your project. – Gilbert Le Blanc Commented Jan 19 at 2:24
  • It threw an exception: can't read input file when i used BufferImage and ImageIO.read(...) – Corvus-OS Commented Jan 19 at 4:20
  • Hello Corvus-OS, I suggest you edit your question and add the directory tree of your project, and the IDE you use, while, you can probe: "src/Images/nintendo.jpg";. – Marce Puente Commented Jan 19 at 5:55
  • 2 How did you "made sure that image path is correct" ? new ImageIcon(String) will create a pseudo image/icon even if the image was not found/loaded - you must check its width or height to confirm it was successfully loaded (it will have a no size if not loaded) - a good reason to use ImageIO (a litle less older than ImageIcon) -- Tutorial How to Use Icons: "If the data location is invalid (but non-null), an ImageIcon is still successfully created; it just has no size and, therefore, paints nothing." – user85421 Commented Jan 19 at 8:58
  • “It threw an exception” —And did you look at that exception? It was telling you exactly what went wrong and why. – VGR Commented Jan 19 at 19:46
 |  Show 5 more comments

2 Answers 2

Reset to default 0

Your code works just fine, if the image path you inputted is indeed correct. On a bare project (without a dependency manager like maven/gradle etc) your file structure should be {ProjectFolder}/Images/nintendo.jpg.

Maybe you are confused about what the Jframe.setIconImage() does and you are expecting something different. It actually changes the top left icon/image of the app (JFrame).

The background JFrame (window) has the default Java Swing icon, while the foreground (highlighted) has a random Nintendo image applied by using the exact same code with the path described as above:

You'll never want to have a relative path in the file system for what is essentially a resource, which will be packaged and deployed with your app. So you should load it as a resource:

import java.awt.EventQueue;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.UIManager;

public class F extends JFrame {
    private void setGui() throws IOException {
        setLocation(0, 100);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setIconImage(ImageIO.read(F.class.getResource("/gvim.png")));
    }

    public static void main(String[] args) throws Exception {
        // Make sure we have a LAF that HAS an icon
        UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
        // Make sure we have nice window decorations.
        JFrame.setDefaultLookAndFeelDecorated(true);
        EventQueue.invokeAndWait(() -> {
            try {
                F f = new F();
                f.setGui();
                f.setSize(200, 200);
                f.setVisible(true);
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
    }

}

发布评论

评论列表(0)

  1. 暂无评论