Alguem saberia me dizer como eu coloco uma imagem como plano de fundo de um gerenciador de layout, no caso, FlowLayout.
Valeu!!!
isto eh independente do layout… arranje uma figura e renomeie para
background.gif e coloce no mesmo diretorio dos teu fontes e rode
o exemplo abaixo
package panel;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
* @author fabiofalci
*/
public class MyPanel extends JPanel {
private Image background;
public MyPanel() {
this.initialize();
}
protected void initialize() {
this.background = this.getImage("background.gif").getImage();
this.setLayout(null);
JTextField textField = new JTextField("Label");
textField.setBounds(20, 20, 100, 22);
this.add(textField);
JButton b = new JButton("Button");
b.setBounds(130, 20, 100, 22);
this.add(b);
}
public ImageIcon getImage(String path) {
URL imageURL = getClass().getResource(path);
if (imageURL == null)
return null;
return new ImageIcon(imageURL);
}
public void paintComponent(Graphics g) {
Dimension dDesktop = this.getSize();
g.clearRect(0, 0, dDesktop.width, dDesktop.height); // limpa a tela
double width = dDesktop.getWidth();
double height = dDesktop.getHeight();
int x = (int)(width - background.getWidth(null)) / 2;
int y = (int)(height - background.getHeight(null)) / 2;
g.drawImage(background, x, y, this);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyPanel panel = new MyPanel();
frame.setContentPane(panel);
frame.setSize(300, 300);
frame.show();
}
}