我的JFrame类中的线程“AWT-EventQueue-0”java.lang.ClassCastException中的exception

我正在模拟生活,我有吃羊的狼,羊吃草。 这很简单,但我不是JFrameJPanel的专家。 这是我的JFrame代码:

 import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*; public class InterfaceGraphique extends JFrame{ private Case[][] caseMemoire = new Case[16][16]; public InterfaceGraphique(){ setSize(800, 825); setTitle("Evolution"); setLocationRelativeTo(null); Container c = getContentPane(); c.setLayout(null); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); for(int i = 0; i<16; i++){ for(int j = 0; j<16; j++){ Case ca = new Case(i*50, j*50); caseMemoire[i][j] = ca; add(ca); } } setVisible(true); } public Case getCase(int i, int j){ return caseMemoire[i][j]; } public class Case extends JPanel{ private int coordX; private int coordY; private JLabel image; public Case(int x, int y){ coordX = x; coordY = y; setBounds(coordX, coordY, 55, 55); image = new JLabel(new ImageIcon("../images/Grass.png")); this.setLayout(new BorderLayout()); this.add(image); } public void setImg(int i){ switch(i){ case 0: this.remove(image); image = new JLabel( new ImageIcon("../images/Grass.png")); this.setLayout(new BorderLayout()); this.add(image); break; case 1: this.remove(image); break; case 2: this.remove(image); image = new JLabel( new ImageIcon("../images/Wolf.png")); this.setLayout(new BorderLayout()); this.add(image); break; case 3: this.remove(image); image = new JLabel( new ImageIcon("../images/Sheep.png")); this.setLayout(new BorderLayout()); this.add(image); break; } } public JLabel getImage(){ return image; } } public void afficheUniv(Univers u){ for (int i=0; i < u.getLignes(); i++){ for (int j=0; j  0){ u.simulation(); evolution.afficheUniv(u); evolution.setVisible(true); } JOptionPane.showMessageDialog(null, "Il n'y a plus d'animaux en vie !", "Fin", JOptionPane.PLAIN_MESSAGE); } 

}

编译javac Interfacegraphique.java工作正常。 但是当我运行我的程序( java InterfaceGraphique )时,我有错误:

 Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException at javax.swing.LayoutComparator.compare(LayoutComparator.java:75) at javax.swing.LayoutComparator.compare(LayoutComparator.java:42) at java.util.TimSort.countRunAndMakeAscending(TimSort.java:356) at java.util.TimSort.sort(TimSort.java:230) at java.util.Arrays.sort(Arrays.java:1512) at java.util.ArrayList.sort(ArrayList.java:1454) at java.util.Collections.sort(Collections.java:175) at javax.swing.SortingFocusTraversalPolicy.enumerateAndSortCycle(SortingFocusTraversalPolicy.java:136) at javax.swing.SortingFocusTraversalPolicy.getFocusTraversalCycle(SortingFocusTraversalPolicy.java:110) at javax.swing.SortingFocusTraversalPolicy.getFirstComponent(SortingFocusTraversalPolicy.java:445) at javax.swing.LayoutFocusTraversalPolicy.getFirstComponent(LayoutFocusTraversalPolicy.java:166) at javax.swing.SortingFocusTraversalPolicy.getDefaultComponent(SortingFocusTraversalPolicy.java:535) at java.awt.FocusTraversalPolicy.getInitialComponent(FocusTraversalPolicy.java:169) at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:420) at java.awt.Component.dispatchEventImpl(Component.java:4752) at java.awt.Container.dispatchEventImpl(Container.java:2292) at java.awt.Window.dispatchEventImpl(Window.java:2739) at java.awt.Component.dispatchEvent(Component.java:4703) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746) at java.awt.EventQueue.access$400(EventQueue.java:97) at java.awt.EventQueue$3.run(EventQueue.java:697) at java.awt.EventQueue$3.run(EventQueue.java:691) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86) at java.awt.EventQueue$4.run(EventQueue.java:719) at java.awt.EventQueue$4.run(EventQueue.java:717) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75) at java.awt.EventQueue.dispatchEvent(EventQueue.java:716) 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) 

你能帮我找到我的问题我不知道我的ClassCastException在哪里。

好的,所以问题相当模糊,但是Swing应用程序的典型问题是:您只能在事件调度线程上执行GUI操作。

这意味着您无法在“主线程”中更改框架(例如添加/删除面板),并且必须通过SwingUtilities.invokeAndWait方法。 如果你不这样做,那么可能会发生各种奇怪的事情,比如NullPointerExceptionConcurrentModificationException以及你所拥有的东西。

如果您像这样更改main()方法,它将无exception地工作:

 public static void main(String[] args) throws InvocationTargetException, InterruptedException { final Univers u = new Univers(16, 16, 5, 5); final InterfaceGraphique evolution = new InterfaceGraphique(); while (u.getNbMoutons() + u.getNbLoups() > 0) { u.simulation(); SwingUtilities.invokeAndWait(new Runnable() { public void run() { evolution.afficheUniv(u); evolution.setVisible(true); } }); } JOptionPane.showMessageDialog(null, "Il n'y a plus d'animaux en vie !", "Fin", JOptionPane.PLAIN_MESSAGE); }