Problemas con el comportamiento de los JButton en java
Estoy intentando aprender java y en este, que es mi primer código hecho por mi desde 0. Me da unos errores que no sé porque da los resultados que da al ejecutarlo.
La lámina que he hecho es la siguiente:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package buscacolor;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
/**
*
* @author Jose Alberto
*/
public class lamina2 extends JPanel {
public ArrayList<String> colores, lista;
public JButton boton;
private JButton boto[]= new JButton[6];
private JPanel pane1;
int posx=30, j=0, i=0;
String elcolor, color;
public lamina2() {
setLayout(new BorderLayout());
juego();
}
public void cargar() {
colores= new ArrayList();
colores.add("#FFFFFF");
colores.add("#000080");
colores.add("#008000");
colores.add("#00FFFF");
colores.add("#FF00FF");
colores.add("#800080");
colores.add("#FFFF00");
colores.add("#FF0000");
colores.add("#FFC0CB");
colores.add("#FFA500");
colores.add("#000000");
colores.add("#5454a8");
}
public void juego() {
cargar();
pane1= new JPanel();
pane1.setLayout(new GridLayout(3,8,3,3));
add(pane1, BorderLayout.CENTER);
Random rnd= new Random();
lista= new ArrayList();
JButton []pulsa= new JButton[4];
for( i=0; i<4; i++) {
j= rnd.nextInt(colores.size());
boto[i]= new JButton("");
//boton.setBounds(175, 175, posx, 20);
boto[i].setName("boto"+i);
boto[i].setBackground(Color.decode(String.format(colores. Get(j));
boto[i].addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
System.out.println(i);
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
});
pane1.add(boto[i]);
posx += 45;
lista.add(colores.get(j));
colores.remove(j);
}
j= rnd.nextInt(lista.size());
elcolor= NomColor(lista.get(j));
boton= new JButton("Busca el color " + elcolor);
add(boton, BorderLayout.NORTH);
}
public String NomColor(String color) {
switch(color) {
case "#FFFFFF":
elcolor= "Blanco";
break;
case "#000080":
elcolor= "Azul";
break;
case "#008000":
elcolor= "Verde";
break;
case "#00FFFF":
elcolor= "Celeste";
break;
case "#FF00FF":
elcolor= "Rosa";
break;
case "#800080":
elcolor= "Rosa oscuro";
break;
case "#FFFF00":
elcolor= "Amarillo";
break;
case "#FF0000":
elcolor= "Rojo";
break;
case "#FFC0CB":
elcolor= "Rojo claro";
break;
case "#FFA500":
elcolor= "Naranja";
break;
case "#000000":
elcolor= "Negro";
break;
case "#5454a8":
elcolor= "Morado";
break;
}
return elcolor;
}
}
Al ejecutar…..
public void mouseClicked(MouseEvent e) {
System.out.println(i);
}
En los cuatro botones el resultado es siempre 4.
Y si pongo……….
@Override
public void mouseClicked(MouseEvent e) {
boto[i].getBackground();
}
Sale los errores….
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
At buscacolor.lamina2$1. MouseClicked(lamina2.java:70)
At java. Awt. AWTEventMulticaster. MouseClicked(AWTEvent Multicaster.java:270)
At java. Awt. Component. ProcessMouseEvent(Component. Jav a:6538)
At javax. Swing. JComponent.processMouseEvent(JComponen t.java:3324)
At java. Awt. Component.processEvent(Component.java:630 0)
At java. Awt. Container.processEvent(Container.java:223 6)
At java. Awt. Component. DispatchEventImpl(Component. Jav a:4891)
At java. Awt. Container. DispatchEventImpl(Container. Jav a:2294)
At java. Awt. Component.dispatchEvent(Component.java:47 13)
At java. Awt. LightweightDispatcher.retargetMouseEvent( Container.java:4888)
At java. Awt. LightweightDispatcher.processMouseEvent(C ontainer.java:4534)
At java. Awt. LightweightDispatcher.dispatchEvent(Conta iner.java:4466)
At java. Awt. Container. DispatchEventImpl(Container. Jav a:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2750 )
At java. Awt. Component.dispatchEvent(Component.java:47 13)
At java. Awt. EventQueue. DispatchEventImpl(EventQueue.j ava:758)
At java. Awt. EventQueue.access$500(EventQueue.java:97)
At java. Awt.EventQueue$3.run(EventQueue.java:709)
At java. Awt.EventQueue$3.run(EventQueue.java:703)
At java. Security. AccessController. DoPrivileged(Native Method)
At java. Security.ProtectionDomain$JavaSecurityAccessI mpl. DoIntersectionPrivilege(ProtectionDomain.java: 76)
At java. Security.ProtectionDomain$JavaSecurityAccessI mpl. DoIntersectionPrivilege(ProtectionDomain.java: 86)
At java. Awt.EventQueue$4.run(EventQueue.java:731)
At java. Awt.EventQueue$4.run(EventQueue.java:729)
At java. Security. AccessController. DoPrivileged(Native Method)
At java. Security.ProtectionDomain$JavaSecurityAccessI mpl. DoIntersectionPrivilege(ProtectionDomain.java: 76)
At java. Awt. EventQueue.dispatchEvent(EventQueue.java: 728)
At java. Awt. EventDispatchThread. PumpOneEventForFilter s(EventDispatchThread.java:201)
At java. Awt. EventDispatchThread. PumpEventsForFilter(E ventDispatchThread.java:116)
At java. Awt. EventDispatchThread. PumpEventsForHierarch y(EventDispatchThread.java:105)
At java. Awt. EventDispatchThread. PumpEvents(EventDispa tchThread.java:101)
At java. Awt. EventDispatchThread.pumpEvents(EventDispa tchThread.java:93)
At java. Awt. EventDispatchThread.run(EventDispatchThre ad.java:82)
Por favor, orientarme un poquito para el ActionListener actúen cada botón de forma individual.