CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 2000
    Posts
    4

    JPanel and Images


    How does one add an image (gif file) to a JPanel?

    Thanks much!


  2. #2
    Join Date
    Sep 2000
    Location
    Melbourne --> Australia
    Posts
    68

    Re: JPanel and Images

    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.



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured