编辑2:
好的,我希望这是您的操作方式。以下是我使用的类的精简版本。如果我在SurfaceObject()类中包含所有的Getter和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);

    }
}


此类会构建容器并将卡片添加到其中(第一张卡片为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。它只是记录按下的按钮
并在按下load File时调用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)


我希望这足以说明问题。提前致谢,

尼古拉

最佳答案

找到了我问题的答案。对于任何想知道的人,似乎Java都对我的SurfaceObject()类的方法“ public FirstOptionPanel getParent()”有问题。我用一个超类的方法向我自己解释这一点。哪种原因导致无法在另一个线程中创建?如果有人有一个更可能的答案,我倾向于倾听。 (Eclipse只是生成了这样的get-method,我自己不会那样做。如果我更改方法名称,则程序可以正常运行)

09-25 20:38