抛出 ClassCastException
ClassCastException thrown
我正在尝试使用 SwingUtilities
创建 JFrame
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();
class Loadingframe很简单JFrame
。有时 运行 这段代码我在
中得到 ClassCastException
new Loadingframe();
我也尝试了 InvokeLater
和 Threads 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.access0(EventQueue.java:97)
at java.awt.EventQueue.run(EventQueue.java:702)
at java.awt.EventQueue.run(EventQueue.java:696)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue.run(EventQueue.java:724)
at java.awt.EventQueue.run(EventQueue.java:722)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.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.access0(EventQueue.java:97)
at java.awt.EventQueue.run(EventQueue.java:702)
at java.awt.EventQueue.run(EventQueue.java:696)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue.run(EventQueue.java:724)
at java.awt.EventQueue.run(EventQueue.java:722)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.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)
并为我加载 class
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();
}
}
我看到你还没有设置平台外观或者你有吗?
像这样
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();
}
}
});
我正在尝试使用 SwingUtilities
JFrame
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();
class Loadingframe很简单JFrame
。有时 运行 这段代码我在
new Loadingframe();
我也尝试了 InvokeLater
和 Threads 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.access0(EventQueue.java:97)
at java.awt.EventQueue.run(EventQueue.java:702)
at java.awt.EventQueue.run(EventQueue.java:696)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue.run(EventQueue.java:724)
at java.awt.EventQueue.run(EventQueue.java:722)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.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.access0(EventQueue.java:97)
at java.awt.EventQueue.run(EventQueue.java:702)
at java.awt.EventQueue.run(EventQueue.java:696)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue.run(EventQueue.java:724)
at java.awt.EventQueue.run(EventQueue.java:722)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.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)
并为我加载 class
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();
}
}
我看到你还没有设置平台外观或者你有吗?
像这样
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();
}
}
});