Link to home
Start Free TrialLog in
Avatar of Zolf
ZolfFlag for United Arab Emirates

asked on

help with inserting image

hello there,

i have this simple frame class to which i want to add a image.how can i do.

cheers
zolf

import javax.swing.*;

import java.awt.*;

public class ImageFrame extends JFrame
{
      public ImageFrame()
      {
            super("My Frame");
            Container c = getContentPane();
            c.setLayout(new BorderLayout());
                        
            setSize(300,300);
            setLocation(100, 100);
            setVisible(true);
      }
      
      public static void main(String [] args)
      {
            new ImageFrame();
      }
}
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

c.add(new JLabel(new ImageIcon("x.jpg")));
Avatar of Zolf

ASKER


i want the frame to be the size of the image.automatically the frame should resize to the image size
Avatar of Zolf

ASKER


i tried this,to some extend it works.i mean the height of the frame is little smaller then the image size.i have to resize the frame to view the image.what could be wrong

ImageIcon im = new ImageIcon("images/7224.jpg");
            c.add(new JLabel(im));
            setSize(im.getIconWidth(),im.getIconHeight());
>>i want the frame to be the size of the image.


c.add(new JLabel(new ImageIcon("x.jpg")));
pack();
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
:-)
Avatar of Zolf

ASKER