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

时间:2016-06-15 10:00:57

标签: java multithreading swing

当我运行我的挥杆应用程序时,有时在开始时我会遇到以下异常:

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.Arrays.sort(Unknown Source)
at java.util.ArrayList.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$500(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$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.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$JavaSecurityAccessImpl.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$500(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$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.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$JavaSecurityAccessImpl.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)

我已找到解决方法,但我不确定这是否完全解决了问题。 如果我改变:

public class MainFrame extends JFrame {
    ...
}
public static void main(String[] args){
    new MainFrame();
}

为:

public static void main(String[] args){
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            new MainFrame();
        }
    });
}

我可以确定这可以完全消除上述异常,并且在事件调度线程之外创建表单是导致错误的唯一原因吗?

以下是我的应用程序的简化代码:

public class App {

    //MyFrame can be show independently or in a TabbedPane
    public static class MyFrame extends JFrame {
        public MyFrame() {
            setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));

            for (int i = 0; i < 8; i++) {
                JPanel panel = new JPanel();
                panel.setBorder(BorderFactory.createTitledBorder("Panel " + i));
                panel.add(new JLabel("label " + i));
                add(panel);
            }

            pack();
            setVisible(true);
        }
    }

    public static class MainTabsFrame extends JFrame {
        public MainTabsFrame() {
            JTabbedPane tabsPane = new JTabbedPane();

            JFrame frame = new MyFrame();
            tabsPane.addTab("My Frame 1", frame.getContentPane());
            frame.setVisible(false);

            add(tabsPane);
            pack();
            setLocationRelativeTo(null);
            setDefaultCloseOperation(EXIT_ON_CLOSE);

            setVisible(true);
        }
    }

    public static void main(String[] args) {
        new MainTabsFrame();
    }
}

1 个答案:

答案 0 :(得分:2)

还需要一行代码。

这是最终解决方案:

public static void main(String[] args){
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            JFrame _mf= new MainFrame();
            _mf.setVisible(true);
        }
    });
}

请记住,涉及Swing对象的每种操作都必须由事件调度线程执行,因此第一个JFrame的创建和可视化也是如此。