Como colocar uma imagem de background em um JPanel

2 respostas
renatoramiro

Este JPanel é um formulário que conterá outros componentes como JButton, JLabel, JTextField e etc.
Gostaria de alguma ajuda para colocar uma imagem de background em um JPanel.

Abraços!!

2 Respostas

lrgalego

Use um JLabel:
http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/JLabel.html

Adicione a imagem no mesmo e então o adicione ao seu JPanel

ivo_costa

Uma gambi:

import java.awt.Dimension;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Teste {
	
	public Teste(){
		JLabel label = new JLabel(new ImageIcon("1.jpg"));
		label.setSize(375, 450);
		label.setLocation(0, 0);
		JFormattedTextField f = new JFormattedTextField();
		f.setSize(250, 30);
		f.setLocation(10, 20);
		JButton b = new JButton("botão");
		b.setSize(100, 30);
		b.setLocation(10, 60);
		
		JFrame frame = new JFrame();
		frame.setLayout(null);
		frame.add(f);
		frame.add(b);
		frame.add(label);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setPreferredSize(new Dimension(375, 450));
		frame.pack();
		frame.setVisible(true);
	}
	
	public static void main(String[] args) {
		new Teste(); 
	}
}
Criado 1 de julho de 2008
Ultima resposta 1 de jul. de 2008
Respostas 2
Participantes 3