[Q#18871150][A#18871400] How do i load images in Jframe java(eclipse)
https://stackoverflow.com/q/18871150
I have an paneel.java file wich looks like the following code: i would like to show the test.gif file. How do i get this done? because when i run it in eclipse i only get the jframe with no image in it.
Answer
https://stackoverflow.com/a/18871400
Use this
APIzation
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
public class ImageInFrame {
public static void main(String[] args) throws IOException {
String path = "Image1.jpg";
File file = new File(path);
BufferedImage image = ImageIO.read(file);
JLabel label = new JLabel(new ImageIcon(image));
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(label);
f.pack();
f.setLocation(200,200);
f.setVisible(true);
}
}
package com.stackoverflow.api;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Human18871400 {
public static void loadImgInFrame(String path) {
File file = new File(path);
BufferedImage image = ImageIO.read(file);
JLabel label = new JLabel(new ImageIcon(image));
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(label);
f.pack();
f.setLocation(200, 200);
f.setVisible(true);
}
}
package com.stackoverflow.api;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
/**
* How do i load images in Jframe java(eclipse)
*
* @author APIzator
* @see <a href="https://stackoverflow.com/a/18871400">https://stackoverflow.com/a/18871400</a>
*/
public class APIzator18871400 {
public static void loadImage(String path) throws IOException {
File file = new File(path);
BufferedImage image = ImageIO.read(file);
JLabel label = new JLabel(new ImageIcon(image));
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(label);
f.pack();
f.setLocation(200, 200);
f.setVisible(true);
}
}