java - 我的 JFrame 类中线程 "AWT-EventQueue-0"java.lang.ClassCastException 中的异常

标签 java swing

我在做一个模拟人生,我有狼吃羊,羊吃草。这很简单,但我不是 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 < u.getColones(); j++) {
            if(u.getCellule(i, j).isLoup())
                caseMemoire[i][j].setImg(2);
            else if(u.getCellule(i, j).isMouton())
                caseMemoire[i][j].setImg(3);
            else if(u.getCellule(i, j).isHerbe())
                caseMemoire[i][j].setImg(0);
            else if(u.getCellule(i, j).isSels())
                caseMemoire[i][j].setImg(1);
            else
                caseMemoire[i][j].setImg(1);
        }
    }

}

public static void main(String[] args){
    Univers u = new Univers (16, 16, 5, 5);
    InterfaceGraphique evolution = new InterfaceGraphique();
    while(u.getNbMoutons()+u.getNbLoups() > 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);
}

关于java - 我的 JFrame 类中线程 "AWT-EventQueue-0"java.lang.ClassCastException 中的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27807079/

相关文章:

java - Univocity - 自动配置不适用于 MultiBeanListProcessor

java - java从结果集中获取数据

java - 如何在使用 GridBagLayout 布局的 JTextPane 中对文本进行换行

java - 暂停和恢复 SwingWorker.doInBackground()

java - Lucene 中的术语文档矩阵

java - Android - 导入时测试 "Well Formed"有效 XML

java - 我可以使用 Stormpath Java SDK 在 strompath 中验证用户帐户吗?

java - 在java中使用mouseevent画线

java - 单击按钮时更改 JEditorPane 内的 html 页面

java - 如何从网页上的链接部署JFrame接口(interface)?