• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Picture not appearing

 
Ola gharably
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Would anybody please tell me what might be wrong with the following lines of code:
import java.awt.*;
import javax.swing.*;
public class Test extends JFrame{
private String name = "image.gif";
private Icon icon = new ImageIcon(name);
private JLabel label;
public Test(){
Container c = getContentPane();
c.setLayout( new FlowLayout() );
label = new JLabel(icon);
c.add(label);
setSize(800,600);
setVisible(true);
}
public static void main (String args[]) {
Test t = new Test(); t.setDefaultCloseOperationJFrame.EXIT_ON_CLOSE);
}
NOTE: the image is in the same directory of the java and class files.
This code should just show my picture, right?? then what might be wrong ??
 
Francis Siu
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ar yes
Ola gharably
Please try to change your code to the following
private ImageIcon icon;
label.setIcon(icon=new ImageIcon("image.gif"));
Hope to solve your problem
 
Ola gharably
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you alot Siu, i've tried your suggestion but unfortunatly it didn't solve my problem :roll:
I'm still with my problem
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope... it should work just the way you have it. Try and load a different image. It may be a problem with the GIF.
 
Francis Siu
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm still with my problem
Get damage!!I do not know why it does not work~~~~
please try to delete
c.setLayout( new FlowLayout() );
change to
label.setBounds(100,100,icon.getIconWidth(),icon.getIconHeight());
:roll:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic