How does one add an image (gif file) to a JPanel?
Thanks much!
Printable View
How does one add an image (gif file) to a JPanel?
Thanks much!
Hi Lisa
Probably the easiest way to do this
is by putting the image inside a JLabel and
then adding it, like the following example:
--------------------------------
import javax.swing.*;
import java.awt.event.*;
class ShowImageFile extends JFrame{
JPanel jay;
JLabel jl;
public static void main(String[] args){
new ShowImageFile();
}
public ShowImageFile(){
setSize(500,500);
jay = new JPanel();
jl = new JLabel(new ImageIcon("TenPeople.jpg"));
jay.add(jl);
getContentPane().add(jay);
setVisible(true);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
}
Gif will work fine also.
Phill.