线程中的异常" AWT-EventQueue-0" java.lang.ClassCastException:add()异常

时间:2014-08-25 20:34:27

标签: java swing exception-handling classcastexception awt-eventqueue

EDIT2: 好的,我希望你就是这样做的。下面是我使用的类的精简版本。如果我在SurfaceObject()classe中包含所有Getters和Setter,则错误仍然存​​在,但如果我将它们排除在外,它似乎会消失。这很奇怪。甚至奇怪的是,如果变量都是私有的,它似乎也会出现,而不是像示例中那样受保护。所以我想知道为什么会这样: 主类(构建JFrame):

import javax.swing.JFrame;
public class FileCreator extends JFrame{
    private static FileCreator chooseAction;
    private Container contain;
    public static final int framewidth = 800;
    public static final int frameheight = 600;
    private final String[] iconName = {"Load File"};
    public static void main(String[] args) {
        chooseAction = new FileCreator();
    }
    public FileCreator(){
        super("Editor");
        contain = new Container(iconName, this);
        add(contain);
        setSize(framewidth,frameheight);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);

    }
}

此类构建容器并将卡添加到其中(First Card是holdContent,  第二张卡是FirstOptionPanel)。它还添加了ActionListener MainListener, 它又调用createPanel() - Method

import java.awt.CardLayout;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JPanel;
class Container extends JPanel {
    private FirstOptionPanel[] panels = new FirstOptionPanel[9];
    private final String[] cardNames = { "card1", "File Loaded" };
    private JButton[] buttons;
    private MainMenuListener main;
    private int contentWidth = 410, contentHeight = 300;
    private CardLayout cards;
    Container(String[] buttonName,
            FileCreator creator) {
        super();
        setLayout(cards = new CardLayout());
        setSize(FileCreator.framewidth, FileCreator.frameheight);
        buttons = new JButton[buttonName.length];
        int i = 0;
        buttons[i] = new JButton(buttonName[i]);
        main = new MainMenuListener(this, creator);
            buttons[i].addActionListener(main);
        JPanel card1 = new JPanel();
        card1.setSize(getWidth(), getHeight());
        card1.setLayout(null);
        JPanel holdContent = new JPanel();
        holdContent.setSize(contentWidth, contentHeight);
        holdContent.setLocation((FileCreator.framewidth - contentWidth) / 2,
                (FileCreator.frameheight - contentHeight) / 2);
        holdContent.add(buttons[i]);
        card1.add(holdContent);
        add(card1, cardNames[0]);
    }
    public JButton[] getButtons() {
        return buttons;
    }
    public void createPanel(int i, File selectedFile) {
        panels[i] = new FirstOptionPanel(selectedFile);
        add(panels[i], cardNames[i + 1]);
        cards.show(this, cardNames[i + 1]);
    }
}

这个类是ActionListener。它只是注册按下的按钮   并在按下加载文件时调用createPanel() - 方法。但是,JFileChooser   在这个版本中什么都不做。通常,它会加载文件并传递它。

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class MainMenuListener implements ActionListener {
    private JButton[] buttons;
    private JPanel[] panellist;
    private Container container;
    private JFileChooser jf;
    private FileCreator creator;
    public MainMenuListener(Container container, FileCreator creator) {
        // TODO Auto-generated constructor stub
        buttons= container.getButtons();
        this.creator = creator;
        panellist = container.getPanels();
        this.container = container;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        for(int i = 0; i<buttons.length;i++){if(i == 0){
                jf = new JFileChooser();
                int returnValue = jf.showOpenDialog(container);     
                if(returnValue == JFileChooser.APPROVE_OPTION){
                    container.createPanel(i, jf.getSelectedFile());
                }else       if(returnValue == JFileChooser.CANCEL_OPTION){
                    JOptionPane.showMessageDialog(creator, "No File selected!");
                }

            }
        }
    }
    }

这是FirstOptionPanel类。它是一个创建其他JPanel的JPanel(在本例中是单个entry-Object),然后显示它们:

import java.io.File;
import javax.swing.JPanel;

public class FirstOptionPanel extends JPanel{
    private SurfaceObject[] entries;
    JPanel buttonHolder;
    public FirstOptionPanel(File selectedFile) {
        // TODO Auto-generated constructor stub
        super();
        setSize(FileCreator.framewidth, FileCreator.frameheight);
        setLayout(null);
        createObjects();
            }
    public FirstOptionPanel() {
        // TODO Auto-generated constructor stub
        super();
    }
    private void createObjects() {
                entries = new SurfaceObject[1];
                    entries[0] = new SurfaceObject("ENTRIES{|,Logo1,,1,,100,,100,,100,,100,|}");
                    add(entries[0]);
                }

}

最后,SurfaceObject类。前面提到的SurfaceEntry-Class扩展了这个类。此处未使用SurfaceEntry(),但仍会出现错误。但只有我拥有所有这些不同的getters()和setters()。如果我不包含它们,程序运行就好了。即使我还没有真正使用它们!(提前大文件):

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.MouseInfo;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class SurfaceObject extends JPanel{
    /**
     * 
     */
    private String iconName;
    private int id;
    int coordinates[] = new int[2];
    int momentaryCoord[] = new int[2];
    protected boolean imageNotFound;
    protected int[] sizeOfRectangle = new int[2];
    protected String pictureName;
    protected ImageIcon momentaryPicture;
    protected Image img;
    protected boolean mouseInAction;
    protected Dimension d;
    protected boolean selected;
    private FirstOptionPanel parent;
    public SurfaceObject(String group) {
        super();
    }

    @Override
    protected void paintComponent(Graphics g) {
        // TODO Auto-generated method stub
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        if (!imageNotFound) {
            g2.drawImage(img, 0, 0, null);
            System.out.println("shown");
        } else {
            System.out.println("Drawn");
            g2.setColor(Color.blue);
            g2.fillRect(0, 0, 200, 200);
        }

    }

    public boolean isSelected() {
        return selected;
    }

    public void setSelected(boolean selected) {
        this.selected = selected;
    }

    public final String getIconName() {
        return iconName;
    }

    public final void setIconName(String iconName) {
        this.iconName = iconName;
    }

    public final int getId() {
        return id;
    }

    public final void setId(int id) {
        this.id = id;
    }

    public final int[] getCoordinates() {
        return coordinates;
    }

    public final void setCoordinates(int[] coordinates) {
        this.coordinates = coordinates;
    }

    public final int[] getMomentaryCoord() {
        return momentaryCoord;
    }

    public final void setMomentaryCoord(int[] momentaryCoord) {
        this.momentaryCoord = momentaryCoord;
    }

    public final boolean isImageNotFound() {
        return imageNotFound;
    }

    public final void setImageNotFound(boolean imageNotFound) {
        this.imageNotFound = imageNotFound;
    }

    public final int[] getSizeOfRectangle() {
        return sizeOfRectangle;
    }

    public final void setSizeOfRectangle(int[] sizeOfRectangle) {
        this.sizeOfRectangle = sizeOfRectangle;
    }

    public final String getPictureName() {
        return pictureName;
    }

    public final void setPictureName(String pictureName) {
        this.pictureName = pictureName;
    }

    public final ImageIcon getMomentaryPicture() {
        return momentaryPicture;
    }

    public final void setMomentaryPicture(ImageIcon momentaryPicture) {
        this.momentaryPicture = momentaryPicture;
    }

    public final Image getImg() {
        return img;
    }

    public final void setImg(Image img) {
        this.img = img;
    }

    public final Dimension getD() {
        return d;
    }

    public final void setD(Dimension d) {
        this.d = d;
    }

    public final boolean isMouseInAction() {
        return mouseInAction;
    }

    public final FirstOptionPanel getParent() {
        return parent;
    }

}

这是我运行程序时收到的错误消息:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException
at javax.swing.LayoutComparator.compare(Unknown Source)
at javax.swing.LayoutComparator.compare(Unknown Source)
at java.util.TimSort.countRunAndMakeAscending(Unknown Source)
at java.util.TimSort.sort(Unknown Source)
at java.util.TimSort.sort(Unknown Source)
at java.util.Arrays.sort(Unknown Source)
at java.util.Collections.sort(Unknown Source)
at javax.swing.SortingFocusTraversalPolicy.enumerateAndSortCycle(Unknown Source)
at javax.swing.SortingFocusTraversalPolicy.getFocusTraversalCycle(Unknown Source)
at javax.swing.SortingFocusTraversalPolicy.getFirstComponent(Unknown Source)
at javax.swing.LayoutFocusTraversalPolicy.getFirstComponent(Unknown Source)
at javax.swing.SortingFocusTraversalPolicy.getDefaultComponent(Unknown Source)
at java.awt.FocusTraversalPolicy.getInitialComponent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.SequencedEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

我希望这足以解释一下。提前谢谢,

尼古拉

1 个答案:

答案 0 :(得分:1)

找到我的问题的答案。对于任何想知道的人来说,似乎Java方法存在问题&#34; public FirstOptionPanel getParent()&#34;我的SurfaceObject() - 类。我向自己解释这是因为它是来自超类的方法。以某种方式使其无法在另一个线程中创建?如果有人有更可能的答案,我倾向于倾听。 (Eclipse刚刚生成了那样的get-method,我自己也不会这样做。如果我改变了方法名,那么程序运行得很好)