Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to paste a image on a frame

 
ankur rathi
Ranch Hand
Posts: 3830
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all ,
I m trying to paste an image on a frame .

Paster() {

setSize(500,500);
Container con = getContentPane();
Icon i = new ImageIcon("c:/hi.gif");
JLabel l = new JLabel(i);
con.add(l);
}

But its not working .
This is a constructor of a class extending JFrame .

Please let me know what wrong in this .
Also provide some code .
Thanks in advance .
 
Edward Chen
Ranch Hand
Posts: 798
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you almost get it. we need add the contentpane to JFrame.

ImageIcon icon = new ImageIcon("ddd.gif");
JLabel label = new JLabel();
label.setIcon(icon);
frame.getContentPane().add(label);
frame.setSize(300,300);
frame.setVisible(true);
 
Pat Hays
Ranch Hand
Posts: 138
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess you must add your JLabel after the JFrame setVisible(true). If so, you should call validate() to show this JLabel.

Change your code as below:
Paster() {
setSize(500,500);
Container con = getContentPane();
Icon i = new ImageIcon("c:/hi.gif");
JLabel l = new JLabel(i);
con.add(l);
this.validate();
}
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic