Click to See Complete Forum and Search --> : JPanel and Images


lisa alben
September 21st, 2000, 07:25 PM
How does one add an image (gif file) to a JPanel?

Thanks much!

Phill
September 21st, 2000, 11:38 PM
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.