Cambiar Color de fondo del JPanel
Hola Amigos del Foro, tengo una pequeña duda en un código, quiere cambiar de fondo un JPanel desde otra clase, desde la misma clase puedo hacerlo sin problemas, pero desde otra clase del JMenuBar no me sale, No se que me falta....
Aquí va el código:
Este es la clase del JPanel:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
public class Paner extends JPanel implements ActionListener{
private Color color = Color.BLUE;
private JButton boton = new JButton("Clic aqui");
public Paner(){
setLayout(null);
setSize(300,300);
setLocation(0,0);
setBackground(getColor());
boton.setSize(100, 40);
boton.setLocation(50, 130);
boton.addActionListener(this);
add(boton);
setVisible(true);
}
/**
* @return the color
*/
public Color getColor() {
return color;
}
/**
* @param color the color to set
*/
public void setColor(Color color) {
this.color = color;
}
//ESTE METODO PUBLICO PARA CAMBIAR EL FONDO DEL JPANEL DESDE OTRA CLASE: que esta mal...
public void fondo(Color color){
setBackground(color);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==getBoton()){
//JOptionPane.showMessageDialog(null, "Holaaaaa");
setColor(Color.CYAN);
setBackground(getColor());
}
}
/**
* @return the boton
*/
public JButton getBoton() {
return boton;
}
/**
* @param boton the boton to set
*/
public void setBoton(JButton boton) {
this.boton = boton;
}
}
Este es la clase del JMenuBar, aqui esta el problema cuando quiero cambiar de fondo al Jpanel mediante el método fondo(Color.RED);
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
public class Menur extends JMenuBar implements ActionListener{
JMenu men= new JMenu("Archivo");
JMenuItem ite= new JMenuItem("Color");
Paner p = new Paner();
public Menur(){
ite.addActionListener(this);
men.add(ite);
add(men);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==ite){
p.fondo(Color.RED);
//JOptionPane.showMessageDialog(null, "Holaaa");
}
}
}
Este es la clase Frame principal que contiene ambos objetos
import javax.swing.GroupLayout;
import javax.swing.JFrame;
public class Framer extends JFrame{
Container f=this.getContentPane();
public Framer(){
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(600,600);
setLocationRelativeTo(null);
setLayout(new GroupLayout(getContentPane()));
add(new Paner());
setJMenuBar(new Menur());
setVisible(true);
}
public static void main(String[] args){
new Framer();
}
}
Lo he puesto en mayusculas el método del Jpanel para cambiarle el fondo, y desde el JMenuBar quiero cambiarle el fondo al hacer clic en el item. Agradezco su ayuda.
- Inicie sesión o regístrese para enviar comentarios
Comentarios recientes
hace 1 semana 1 día
hace 7 semanas 6 días
hace 1 año 4 días
hace 2 años 12 semanas
hace 2 años 16 semanas
hace 2 años 23 semanas
hace 2 años 31 semanas
hace 2 años 34 semanas
hace 2 años 36 semanas
hace 2 años 39 semanas