java swing jframe jpanel imageicon

java swing image



Java agregando ImageIcon a JLabel (3)

Tratar,

ImageIcon image = new ImageIcon("c://path//image.png"); imagelabel = new JLabel(character, image, JLabel.CENTER); frame.add(imagelabel);

Eche un vistazo a Tutorial - Cómo usar iconos

Intento hacer un juego muy básico con Java y tengo problemas para mostrar una imagen en un JFrame . Ha funcionado en el pasado para mí y ahora no, no puedo ver lo que hice mal.

Intenté imprimir el directorio de trabajo actual y cambiar dónde obtengo mi imagen para que coincida con eso. Es probable que el problema no sea obtener la imagen, ya que mi (buscador de archivo o lector de archivos o algo así) puede encontrarlo sin problemas, pero no puedo agregarlo correctamente (el ImageIcon ) a JLabel , o al JFrame .

Este es mi código ...

JFrame frame = new JFrame("no image"); ImageIcon image = new ImageIcon("C:/Documents and Settings/user/Desktop/hi/xD/JavaApplication2/image.png"); JLabel imagelabel = new JLabel(image); frame.add(imagelabel);

El JFrame ha sido setVisible(true) y pack() .

¿Podría alguien ayudarme a entender qué está mal?


Tu problema yace aquí:

ImageIcon image = new ImageIcon("C:/Documents and Settings/user/Desktop/hi/xD/JavaApplication2/image.png"); JLabel imagelabel = new JLabel(character);

Usted crea una "imagen" ImageIcon pero crea su JLabel con "personaje".

Debería ser:

JLabel imagelabel = new JLabel(image);


import javax.awt.*; import java.awt.*; import java.awt.event*; //class name image class image { image() //constructor { Frame f=new Frame("Image"); //Frame f.setSize(500,500); f.setVisible(true); Panel p =new Panel(); //Panel f.add(p); p.addLayout(null); ImageIcon ii=new ImageIcon("set your image path"); //ImageIcon is used to image Display . Label l =new Label(ii); p.add(ii); p.setBounds(set you bounds); //Like that(20,20,500,40); } public static void main(String [] args) { image obj = new } }