Wenn ich meine Swing-Anwendung ausführen, manchmal am Anfang habe ich Ausnahme folgende:Java Exception in thread "AWT-Eventqueue-0" java.lang.ClassCastException
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)
ich Lösung dieses Problems gefunden haben, aber Ich bin mir nicht sicher, ob dies das Problem vollständig löst. Wenn ich ändern:
public class MainFrame extends JFrame {
...
}
public static void main(String[] args){
new MainFrame();
}
zu:
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new MainFrame();
}
});
}
kann ich oben Ausnahme sicher sein, das vollständig von auftretenden eliminiert, und die außerhalb des Event Dispatch Thread zu schaffen Form wurde der einzige Grund, verursacht Fehler?
ist hier Code für meine Anwendung vereinfacht:
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();
}
}
Bitte geben Sie eine [SSCCE] (http://sscce.org) an, damit wir Ihnen helfen können. –
können Sie klären, was Sie mit "manchmal am Anfang" meinen? – ChadNC
Fehler beim Starten der Anwendung. Ich kann nicht genau sagen, wann es in Schwung EDT geworfen wird, außerdem erzeugen nicht alle Läufe Fehler. – hal