Foros del Web » Programación para mayores de 30 ;) » Java »

JPanel poner fondo no me funciona

Estas en el tema de JPanel poner fondo no me funciona en el foro de Java en Foros del Web. 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 ...
  #1 (permalink)  
Antiguo 08/02/2013, 23:54
 
Fecha de Ingreso: junio-2012
Mensajes: 32
Antigüedad: 11 años, 10 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();

}
}

Etiquetas: jpanel, pintar
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 04:01.