Background image

2 respostas
makoto

estou entrando agora na interface gráfica e queria saber como colocar uma imagem de fundo em um Label. jha até peguei um bom exemplo de como fazer. A dúvida é:

public void paintComponent(Graphics g) {
ImageIcon img = new ImageIcon(“image.jpeg”);

esse image.jpeg é o endereço da imagem, ou apenas o nome do arquivo?
se ficar entre “”(aspas) não será considerado uma String comum?
onde ou devo deixar o arquivo de imagem no computador para acessar ele?

agradeço.

2 Respostas

homisinho
jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/fundo.png")));

Aqui pega uma pasta “imagens” a imagem “fundo”… claro q a pasta imagem esta junto do seu projeto… por isso o getClass().getResource()…

fiaux

Leia a API para entender. Se tiver mais dúvidas pergunte aqui. http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/ImageIcon.html

<blockquote>Constructor Summary

ImageIcon()

Creates an uninitialized image icon.

ImageIcon(byte[] imageData)

Creates an ImageIcon from an array of bytes which were read from an image file containing a supported image format, such as GIF or JPEG.

ImageIcon(byte[] imageData, String description)

Creates an ImageIcon from an array of bytes which were read from an image file containing a supported image format, such as GIF or JPEG.

ImageIcon(Image image)

Creates an ImageIcon from an image object.

ImageIcon(Image image, String description)

Creates an ImageIcon from the image.

ImageIcon(String filename)

Creates an ImageIcon from the specified file.

ImageIcon(String filename, String description)

Creates an ImageIcon from the specified file.

ImageIcon(URL location)

Creates an ImageIcon from the specified URL.

ImageIcon(URL location, String description)

Creates an ImageIcon from the specified URL.</blockquote>
Criado 28 de maio de 2008
Ultima resposta 28 de mai. de 2008
Respostas 2
Participantes 3