Ver Mensaje Individual
  #1 (permalink)  
Antiguo 08/02/2013, 23:54
baterista41
 
Fecha de Ingreso: junio-2012
Mensajes: 32
Antigüedad: 11 años, 11 meses
Puntos: 1
Pregunta JPanel poner fondo no me funciona

Amigos ando viendo muchos tutoriales y buscado en google pero no encuentro código alguno que me funcione lo único que quiero es que

Tengo un JFrame dentro del JFrame se almacena el JPanel y que en el JPanel se le agregue una imagen de fondo para que asi pueda yo agregar mis botones y TextField dentro del panel


Mi clase de fondo es esta

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class JPanelImagen extends JPanel{
private Image fondo;
private ImageIcon icono;


protected void painComponent(Graphics g){
if(fondo!=null){
Graphics2D g2=(Graphics2D)g;
g2.drawImage(fondo,0,0,this.getWidth(),this.getHei ght(),null);
}
}
public ImageIcon getIco(){
return icono;
}

public void setIcon(ImageIcon icono){
this.icono=icono;
if(icono!=null) {
fondo=icono.getImage();
}
}
}




Mi clase donde tengo la ventana y mi botón

import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class Vista {
JPanel uno;
JButton r;
ImageIcon Imagen;
JLabel iimagen;
JPanelImagen panel;
JFrame g;
public Vista(){
g=new JFrame();
g.setTitle("PRIMERA VENTANA");
g.setSize(400,400);
g.setLocation(500,150);
g.setBackground(Color.GREEN);
g.setLayout(null);
g. setResizable(false);
Container getContentPane=g.getContentPane();



cosas();

g.setVisible(true);

g.addWindowListener(new Cerrar());

g.getContentPane().setBackground(Color.WHITE);
}

public void cosas(){
/*Imagen=new ImageIcon(getClass().getResource("pulsa.JPG"));
iimagen=new JLabel(Imagen);
this .iimagen.setBounds(0,0,400,400);
this.getContentPane().add(iimagen);*/

panel=new JPanelImagen();
panel.setIcon(new ImageIcon(panel.getClass().getResource("pulsa.jpg" )));



panel.setBounds(10,10,200,200);
//uno.setBackground(Color.YELLOW);
panel.setLayout(new FlowLayout());


r=new JButton("Presioname");
r.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent s){
new Vista2().setVisible(true);
g.setVisible(false);
}

});


panel.add(r);


g.getContentPane().add(panel);

}

public static void main(String args[]){
Vista x=new Vista();

}
}