Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 169
  • Last Modified:

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();
      }
}
0
zolf
Asked:
zolf
  • 4
  • 3
1 Solution
 
CEHJCommented:
c.add(new JLabel(new ImageIcon("x.jpg")));
0
 
zolfAuthor Commented:

i want the frame to be the size of the image.automatically the frame should resize to the image size
0
 
zolfAuthor Commented:

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());
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
CEHJCommented:
>>i want the frame to be the size of the image.


c.add(new JLabel(new ImageIcon("x.jpg")));
pack();
0
 
CEHJCommented:
(and remove setSize)
0
 
CEHJCommented:
:-)
0
 
zolfAuthor Commented:
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now