本文介绍了线程中的异常“AWT-EventQueue-0”我的JFrame类中的java.lang.ClassCastException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  import java.awt。*; 
import java.awt.event。*;
import javax.swing。*;
import java.io. *;

公共类InterfaceGraphique扩展JFrame {
private Case [] [] caseMemoire = new Case [16] [16];

public InterfaceGraphique(){
setSize(800,825);
setTitle(Evolution);
setLocationRelativeTo(null);
容器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, Ĵ* 50);
caseMemoire [i] [j] = ca;
add(ca);
}
}
setVisible(true);
}

public Case getCase(int i,int j){
return caseMemoire [i] [j];
}

公共类Case扩展JPanel {
private int coordX;
private int coordY;
私人JLabel图片;

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);
休息;
案例1:
this.remove(图片);
休息;
案例2:
this.remove(图片);
image = new JLabel(new ImageIcon(../ images / Wolf.png));
this.setLayout(new BorderLayout());
this.add(image);
休息;
案例3:
this.remove(图片);
image = new JLabel(new ImageIcon(../ images / Sheep.png));
this.setLayout(new BorderLayout());
this.add(image);
休息;
}
}

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 )时,我有错误:

 线程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(LayoutFocusTraver) salPolicy.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)
在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应用程序的一个典型问题:你只能在事件派遣线程。



这意味着你不能改变你的框架(例如在主线程中添加/删除面板,并且必须通过 SwingUtilities.invokeAndWait 方法。如果你不这样做,那么会发生各种奇怪的事情,比如 NullPointerException ConcurrentModificationException ,显然也是你有的东西。



如果你改变这样的 main()方法,它将无异常地工作:

  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);
}


I'm doing a simulation of life, I have wolf who eat sheep, sheep eat grass. It's simple but i'm not an expert in JFrame and JPanel stuff. This is my JFrame code:

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);
}

}

Compilation javac Interfacegraphique.java works fine. But when I run my program (java InterfaceGraphique) I have the error:

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)

Can you help me to find my problem i don't know where is my ClassCastException.

解决方案

OK so the problem is rather obscure but is a typical problem with Swing applications: you can do GUI operations only on the Event Dispatch Thread.

This means that you cannot alter your frame (e.g. add/remove panels) in your "main thread" and have to go via the SwingUtilities.invokeAndWait method. If you don't do this then all sorts of strange things can happen such as NullPointerException, ConcurrentModificationException and apparently also the thing you had.

If you change your main() method like this, it will work without the 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);
}

这篇关于线程中的异常“AWT-EventQueue-0”我的JFrame类中的java.lang.ClassCastException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 12:22