Imagem de fundo no Container

1 resposta
Lindberg

Ola Pessoal …

Estou com um probleminha !!!

Como colocar uma imagem de fundo nessa classe ?

Estou pesquisando muuuuito e não consigo nada 

vejam o codigo abaixo 

//////////////////////////////////////////////////////

import java.awt.<em>;

import java.awt.event.</em>;

import javax.swing.*;

import javax.swing.Box;
import java.awt.Container;

import java.awt.GridBagConstraints;

import java.awt.GridBagLayout;

import javax.swing.JButton;
import javax.swing.JFrame;

public class teste3 extends JFrame {

public JTextField[] tf = new JTextField[55];
private javax.swing.JPanel jPanelG, jPanel1, jPanel2;

public teste3() {

super("teste3");

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

/////////////////////////////////////////////////
JPanel contentPane = new JPanel(new BorderLayout());

JDesktopPane desktop = new JDesktopPane(){  
      Image im = (new ImageIcon("imagens/logo3.jpg")).getImage();   
      public void paintComponent(Graphics g){          
      g.drawImage(im,0,0,this);            }  
                                  };//fim do JDesktopPane  
    contentPane.setOpaque(true);  

    contentPane.add(desktop);//adiciono o JDesktopPane ao JPanel   
//	this.add(contentPane);

///////////////////////////////////////////////////////

jPanelG = new javax.swing.JPanel();
jPanel1 = new javax.swing.JPanel();
jPanel2 = new javax.swing.JPanel();
jPanel1.setLayout(new GridLayout( tf.length, 1 ) ); 
for (int i = 0; i &lt tf.length ; i++)
{
	tf[i] = new JTextField("dad "+i, 5);
		tf[i].setForeground(Color.white);  //(190, 190, 153)
		tf[i].setBackground(Color.black);
		tf[i].setBorder(javax.swing.BorderFactory.createLineBorder(Color.black, 6));  //RED , BLACK
		tf[i].setFont(new java.awt.Font("Arial", 1, 15));
	jPanel1.add(tf[i]);
}

jPanel2.setLayout(new GridLayout( tf.length, 1 ) ); 
for (int i = 0; i &lt tf.length ; i++)
{
	tf[i] = new JTextField("dados "+i+25, 5);
		tf[i].setBackground(Color.blue);
		tf[i].setBorder(javax.swing.BorderFactory.createLineBorder(Color.RED, 4));  //RED , BLACK

	jPanel2.add(tf[i]);
}



Container c = this.getContentPane();

GridBagConstraints cons = new GridBagConstraints();
GridBagLayout layout = new GridBagLayout();
c.setLayout(layout);

cons.fill = GridBagConstraints.BOTH;
cons.gridy = 0;
cons.gridx = 0;
cons.gridwidth = 2;
c.add(new JButton("Esquerda"), cons);
cons.gridwidth = 1;
cons.gridx = 2;
c.add(new JButton("Direita"), cons);

cons.gridx = 0;
cons.gridy = 1;
c.add(new JButton("Esquerda"), cons);

cons.gridx = 1;
c.add(new JButton("Meio"), cons);

cons.gridx = 3;
c.add(new JButton("Direita"), cons);

cons.gridy = 2;
cons.gridx = 0;

cons.ipadx = 300;  
cons.ipady = 250;
//   cons.insets = new Insets(10,10,10,10);  //espaco entre os quadros

//   cons.weightx = 1;

//   cons.weighty = 1;
jPanelG.add(jPanel1);
jPanelG.add(jPanel2);


Component comp = new JScrollPane(jPanelG);  // coloca scroll no jpanel
c.add(comp, cons );




this.setSize(800, 800);

}

public static void main(String[] args) {

teste3 exe = new teste3();

exe.show();

}

}

///////////////////

Valeu

1 Resposta

ViniGodoy

Você tem que sobrescrever o método paintComponent do seu container.

Eu já fiz um JPanel que faz isso, está implementado nesse post:
http://www.guj.com.br/posts/list/56248.java#295271

Criado 30 de agosto de 2007
Ultima resposta 30 de ago. de 2007
Respostas 1
Participantes 2