jLabel.setVisible(true)不起作用

时间:2016-04-19 07:45:20

标签: java jlabel

public class Game extends javax.swing.JFrame {
ArrayList<Integer> numere = new ArrayList<>();
ArrayList<Bila> balls = new ArrayList<Bila>();
ArrayList<String> culori = new ArrayList<>();
ArrayList<JLabel> colours = new ArrayList<>();
Random random = new Random();
JFrame frame = new JFrame("Display Image");
JPanel panel = (JPanel)frame.getContentPane();
int nrBalls=0;

public void createColours(){
    for(int i=0;i<7;i++){
        culori.add("Portocaliu");
        culori.add("Rosu");
        culori.add("Albastru");
        culori.add("Verde");
        culori.add("Negru");
        culori.add("Galben");
        culori.add("Violet");
    }
}
public void createNumbers(){
    for(int i=1;i<50;i++){
        numere.add(i);
    }
}
public void createBalls(){
    while(nrBalls<36){
        int indx =random.nextInt(numere.size());
        int nr = numere.get(indx);
        numere.remove(indx);
        String culoare = culori.get(indx);
        culori.remove(indx);
        balls.add(new Bila(culoare,nr));
        nrBalls++;
    }
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {                                      
    for(int i=0;i<35;i++){
        if(balls.get(i).culoare.equals("Portocaliu")){
            jLabel2.setVisible(true);
        }
    }

} 
 public static void main(String args[]) {
Game game = new Game();
game.createColours();
game.createNumbers();
game.createBalls();
game.afisare();
}  
}
public class Bila {
int nr;
String culoare;
Bila(String culoare,int nr){
    this.nr = nr;
    this.culoare = culoare;
}

当我点击按钮时,我希望它在数组中找到具有相应颜色的第一个球,并显示jLabel。我得到一个IndexOutOfBoundsException with index:0 size:0。有谁知道是什么原因造成的?我的数组列表不是空的,我做了一个方法来打印出数组中的所有元素,它不是空的。 堆栈跟踪:

Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    at java.util.ArrayList.rangeCheck(ArrayList.java:653)
    at java.util.ArrayList.get(ArrayList.java:429)
    at turbo6.Game.jButton1MouseClicked(Game.java:154)
    at turbo6.Game.access$100(Game.java:15)
    at turbo6.Game$2.mouseClicked(Game.java:101)
    at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:270)
    at java.awt.Component.processMouseEvent(Component.java:6538)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6300)
    at java.awt.Container.processEvent(Container.java:2236)
    at java.awt.Component.dispatchEventImpl(Component.java:4891)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Component.dispatchEvent(Component.java:4713)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4534)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
    at java.awt.Container.dispatchEventImpl(Container.java:2280)
    at java.awt.Window.dispatchEventImpl(Window.java:2750)
    at java.awt.Component.dispatchEvent(Component.java:4713)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java: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$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.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$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

0 个答案:

没有答案