抛出ClassCastException

时间:2016-04-20 11:13:36

标签: java eclipse swingutilities

我正在尝试使用JFrame

创建SwingUtilities
Thread tt = new Thread(new Runnable()
    {
        public void run()
        {
            try
            {
                SwingUtilities.invokeAndWait(new Runnable()
                {

                    @Override
                    public void run()
                    {
                        new Loadingframe();

                    }
                });
            } catch (InvocationTargetException e)
            {
                e.printStackTrace();
            } catch (InterruptedException e)
            {
                e.printStackTrace();
            }

        }
    });
    tt.start();

类Loadingframe是一个非常简单的JFrame。有时在运行此代码时,我在

中得到ClassCastException
new Loadingframe();

我也尝试了InvokeLaterThreads only without Swing但同样的错误

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.binarySort(TimSort.java:292)
at java.util.TimSort.sort(TimSort.java:217)
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:751)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:702)
at java.awt.EventQueue$3.run(EventQueue.java:696)
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:724)
at java.awt.EventQueue$4.run(EventQueue.java:722)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:721)
at java.awt.SequencedEvent.dispatch(SequencedEvent.java:128)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:749)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:702)
at java.awt.EventQueue$3.run(EventQueue.java:696)
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:724)
at java.awt.EventQueue$4.run(EventQueue.java:722)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:721)
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)

和我的装载课程

public class Loadingframe extends JFrame
{
    public Loadingframe()
    {
        this.setType(Type.UTILITY);
        this.setUndecorated(true);
        setBackground(new Color(0, 0, 0, 0));
        setSize(new Dimension(500, 500));

        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new GridBagLayout());
        JButton b = new JButton(
                new ImageIcon(getClass().getResource("/reso/loading11.png")));
        b.setBorderPainted(false);
        b.setFocusPainted(false);
        b.setContentAreaFilled(false);
        add(b);
        this.setVisible(true);
        this.addWindowFocusListener(new WindowFocusListener()
        {

            @Override
            public void windowLostFocus(WindowEvent e)
            {
                Loadingframe.this.dispose();
            }

            @Override
            public void windowGainedFocus(WindowEvent e)
            {
            }
        });
        this.setFocusable(true);
        getContentPane().requestFocus();
    }
}

1 个答案:

答案 0 :(得分:1)

我看到你没有设置平台的外观和感觉还是有你?

喜欢这个

try { UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    } catch (Exception e) { }

由于Swing组件未在Swing维护的Componet Hierarchy上注册,因此有时会导致间歇性错误。通常,当我们在除EDT(Event Dispatcher Thread)之外的其他线程上加载GUI内容时会发生这种情况。 要确保注册组件,请执行以下操作,

 Thread tt = new Thread(new Runnable()
    {
        public void run()
        {
            try
            {
                SwingUtilities.invokeAndWait(new Runnable()
                {

                    @Override
                    public void run()
                    {
                        EventQueue.invokeLater(new Runnable() { public void run() {
                            new Loadingframe();

                        }});

                    }
                });
            } catch (InvocationTargetException e)
            {
                e.printStackTrace();
            } catch (InterruptedException e)
            {
                e.printStackTrace();
            }

        }
    });