import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class PanelBackgroungTresColores implements ActionListener{
private Frame f=new Frame();
private Button amarillo, azul,rojo;
public PanelBackgroungTresColores()
{
f.setTitle("Ejercicio en casa");
f.setSize(400,200);
f.setLayout(new BorderLayout());
f.addWindowListener(new Manejador());
Panel p=new Panel();
p.setLayout(new GridLayout(1,3));
amarillo=new Button("amarillo");
azul=new Button("azul");
rojo=new Button("rojo");
amarillo.addActionListener(this);
rojo.addActionListener(this);
azul.addActionListener(this);
p.add(amarillo);
p.add(azul);
p.add(rojo);
Panel p2=new Panel();
p2.setLayout(new GridLayout(4,5));
for(int i=0; i<=10;i++)
{
p2.add(new Button("Nº" + (i+1)));
}
f.add("North",p);
f.add("East",p2);
f.pack();
f.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==amarillo)
{
f.setBackground(Color.yellow);
}
else if(e.getSource()==azul)
{
f.setBackground(Color.blue);
}
else if(e.getSource()==rojo)
{
f.setBackground(Color.red);
}
if (e.getActionCommand()=="amarillo")
{
System.out.println("El color el "+e);
}
}
public static void main(String[] args) {
PanelBackgroungTresColores e=new PanelBackgroungTresColores();
}
}
No hay comentarios:
Publicar un comentario