“比较方法违反了它的一般约定!"- TimSort 和 GridLayout [英] "Comparison method violates its general contract!" - TimSort and GridLayout
问题描述
我制作了一个带有 jPanel 和 JLabel 数组的调色板.起初它运行良好,但后来我将一些其他 jLabels 从 JPanel 中取出并添加了一些事件.现在我不断收到此错误:
I made a color palette with a jPanel and a JLabel array in it. At first it worked well, but then i put some other jLabels out of the JPanel and added them some events. Now I keep getting this error:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Comparison method violates its general contract!
at java.util.TimSort.mergeLo(TimSort.java:747)
at java.util.TimSort.mergeAt(TimSort.java:483)
at java.util.TimSort.mergeCollapse(TimSort.java:410)
at java.util.TimSort.sort(TimSort.java:214)
at java.util.TimSort.sort(TimSort.java:173)
at java.util.Arrays.sort(Arrays.java:659)
at java.util.Collections.sort(Collections.java:217)
at javax.swing.SortingFocusTraversalPolicy.enumerateAndSortCycle(SortingFocusTraversalPolicy.java:136)
at javax.swing.SortingFocusTraversalPolicy.getFocusTraversalCycle(SortingFocusTraversalPolicy.java:110)
at javax.swing.SortingFocusTraversalPolicy.getFirstComponent(SortingFocusTraversalPolicy.java:435)
at javax.swing.LayoutFocusTraversalPolicy.getFirstComponent(LayoutFocusTraversalPolicy.java:166)
at javax.swing.SortingFocusTraversalPolicy.getDefaultComponent(SortingFocusTraversalPolicy.java:515)
at java.awt.FocusTraversalPolicy.getInitialComponent(FocusTraversalPolicy.java:169)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:380)
at java.awt.Component.dispatchEventImpl(Component.java:4731)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:682)
at java.awt.EventQueue$3.run(EventQueue.java:680)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:696)
at java.awt.EventQueue$4.run(EventQueue.java:694)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:693)
at java.awt.SequencedEvent.dispatch(SequencedEvent.java:116)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:721)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:682)
at java.awt.EventQueue$3.run(EventQueue.java:680)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:696)
at java.awt.EventQueue$4.run(EventQueue.java:694)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:693)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)
我在第一次收到此错误后尝试删除我所做的所有事情,但仍然继续出现.当我将布局从 GridLayout 更改为其他任何内容时,错误就会消失,但代码变得无用.所以我需要GridLayout.当我将该 JPanel 中的所有内容移动到另一个 JPanel 时,错误也会消失.但是当我删除第一个 JPanel 时,错误又回来了.
I tried to remove everything i've done after first time i got this error, but still keep getting it. When i change the layout from GridLayout to anything else, then the error disappears, but the code becomes useless. So i need GridLayout. When i move everything in that JPanel to another JPanel, the error also goes away. But when i remove the first JPanel, error comes back.
顺便说一下,程序可以运行,但不断出现错误令人不快......
By the way, the program works, but it's not pleasent to keep getting errors...
当我使用少于 225 种颜色时,没有错误.我真的很好奇发生了什么.任何解释将不胜感激...
When i use less than 225 color, there's no error. I'm really curious about what's happening. Any explanation would be appreciated...
推荐答案
在我看来,您似乎遇到了 JDK 中的错误,因为错误似乎来自 Swing 类.
It seems to me like you've hit a bug in the JDK since the error seems to come from Swing classes.
选项:
将属性
java.util.Arrays.useLegacyMergeSort
定义为true
.在您的代码中使用该行
Define the property
java.util.Arrays.useLegacyMergeSort
astrue
. Either using in your code the line
System.setProperty("java.util.Arrays.useLegacyMergeSort", "true");
在任何 Swing 代码之前.main
方法中的第一行应该可以工作.
before any Swing code. As the first line in the main
method should work.
或添加
-Djava.util.Arrays.useLegacyMergeSort=true
到您的启动选项(在控制台中,或在 IDE、Ant 脚本等中的项目属性中)
to your starting options (in the console, or in the project properties in an IDE, Ant script, etc.)
升级你的JDK,看看问题是否消失
Upgrade your JDK and see if the problem goes away
这篇关于“比较方法违反了它的一般约定!"- TimSort 和 GridLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!