B
B
barabash20902022-02-19 17:53:28
Java
barabash2090, 2022-02-19 17:53:28

How to add an image in Java Swing?

Good afternoon! I am developing a Java Swing application using the GUI in Intellij IDEA. Got a question. In the constructor, I have created a JPanel. You need to add an image to it, which will be stretched across the entire panel. And also this image will then change to another image or video. I add the image through the code, not through the constructor. Error thrown:
Exception in thread "main" java.lang.ClassCastException: class java.lang.String cannot be cast to class com.intellij.uiDesigner.core.GridConstraints (java.lang.String is in module java.base of loader ' bootstrap'; com.intellij.uiDesigner.core.GridConstraints is in unnamed module of loader 'app')
at com.intellij.uiDesigner.core.GridLayoutManager.addLayoutComponent(GridLayoutManager.java:133)

ImageIcon image = new ImageIcon("rocket.jpg");
        JLabel label = new JLabel("", image, JLabel.CENTER);

        monitorPanel.add(label, BorderLayout.CENTER);

Advise, please, the competent decision.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question