• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Picture not appearing

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ??
 
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
a fool thinks himself to be wise, but a wise man knows himself to be a fool - shakespeare. foolish tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic