[英]java Exception in thread “AWT-EventQueue-0” java.lang.ClassCastException
When i run my swing application, sometimes at the beginning i got following exception: 当我运行我的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)
I have found solution to this, but i am not sure if this completely solves the problem. 我已经找到解决方案,但是我不确定这是否可以完全解决问题。 If i change:
如果我改变:
public class MainFrame extends JFrame {
...
}
public static void main(String[] args){
new MainFrame();
}
to: 至:
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new MainFrame();
}
});
}
can i be sure this eliminates above exception from occuring completely, and that creating form outside The Event Dispatch Thread was the only reason causing error? 我可以确定这完全消除了上述异常,并且在事件调度线程之外创建表单是导致错误的唯一原因吗?
Here is simplified code for my application: 这是我的应用程序的简化代码:
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();
}
}
It is necessary one more line of code. 需要多一行代码。
This is the final solution: 这是最终的解决方案:
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame _mf= new MainFrame();
_mf.setVisible(true);
}
});
}
Please remember that every kind of operation that involves a Swing object has to be performed by the Event Dispatch Thread , so also the creation and visualization of the first JFrame. 请记住,涉及Swing对象的每种操作都必须由Event Dispatch Thread执行 ,因此第一个JFrame的创建和可视化也必须执行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.