domingo, 26 de febrero de 2012

JButton y background


package ejercicios;

import java.awt.Button;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;



public class EjercicioJButton extends Frame implements ActionListener{

private Frame f=new Frame();
private Button azul,amarillo,rosa,verde;

public EjercicioJButton()
{
f.setTitle("hola");
f.setSize(500,300);
f.setLayout(new FlowLayout());
f.addWindowListener(new Manejador());


azul=new Button("Azul");
Dimension d=new Dimension(50,25);
azul.setPreferredSize(d);
azul.addActionListener(this);

rosa=new Button("Rosa");
rosa.setPreferredSize(d);
rosa.addActionListener(this);

amarillo=new Button("Amarillo");
amarillo.setPreferredSize(d);
amarillo.addActionListener(this);

verde=new Button("Verde");
verde.setPreferredSize(d);
verde.addActionListener(this);
f.addMouseListener(new MouseListener(){

@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub

}

@Override
public void mouseEntered(MouseEvent arg0) {
f.setBackground(Color.BLACK);

}

@Override
public void mouseExited(MouseEvent arg0) {
f.setBackground(Color.CYAN);

}

@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub

}

@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub

}

});




f.add(rosa);
f.add(amarillo);
f.add(verde);
f.add(azul);

f.setVisible(true);
}
public static void main(String[] args) {
EjercicioJButton b=new EjercicioJButton();

}

public void actionPerformed(ActionEvent e) {
Object source=e.getSource();
if(source==azul)
f.setBackground(Color.blue);
if(source==rosa)
f.setBackground(Color.PINK);
if(source==amarillo)
f.setBackground(Color.yellow);
if(source==verde)
f.setBackground(Color.green);

}


}

No hay comentarios:

Publicar un comentario