找不到例外的原因

时间:2016-09-01 12:56:58

标签: java swing exception illegalargumentexception

我试图找到一些与我的问题相关的已回答问题,但我没有成功。

在我的应用程序中,我得到一个奇怪的java.lang.IllegalArgumentException,但我无法找到原因,因为在堆栈跟踪中没有任何引用任何代码行。 我也试图以我所知道的各种方式捕捉异常,但仍然没有......

这是我的代码:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
public class Test
{
    public static void main(String[] a){
        SwingUtilities.invokeLater(new Runnable(){
            public void run(){
                try{
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    JFrame frame = new JFrame("Palette Frame");
                    frame.add(new PalettePanel());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
                catch(Exception ex){
                    ex.printStackTrace();
                }
            }
        });
    }
}
class PalettePanel extends JPanel
{
    public PalettePanel(){
        ArrayList<Color> colors = new ArrayList<Color>();
        int[] values = {0,64,128,192,255};
        for(int red:values)for(int green:values)for(int blue:values)colors.add(new Color(red,green,blue));
        JPanel palette = new JPanel(new GridLayout(5,25,2,2));
        for(Color color:colors)palette.add(new ColoredButton(color));
        add(palette);
    }
    private class ColoredButton extends JButton
    {
        public ColoredButton(Color background){
            setBackground(background);
            setBorder(new LineBorder(Color.BLACK));
            setContentAreaFilled(false);
            setMargin(new Insets(10,10,10,10));
            setOpaque(true);
        }
    }
}

这就是我得到的:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Comparison method violates its general contract!
        at java.util.TimSort.mergeLo(Unknown Source)
        at java.util.TimSort.mergeAt(Unknown Source)
        at java.util.TimSort.mergeCollapse(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$1.doIntersectionPrivilege(Unknown Source)
        at java.security.ProtectionDomain$1.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$1.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$1.doIntersectionPrivilege(Unknown Source)
        at java.security.ProtectionDomain$1.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$1.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)

感谢您的帮助!!

[编辑]

首先,感谢你的所有评论!

我只是想对我的代码发布一个小更新,这些小改动我没有得到任何异常,也许它可以帮助有同样问题的人......

如果我用这个替换ColoredButton之前的构造函数:

public ColoredButton(Color background){
            setBackground(background);
            setBorder(new CompoundBorder(new LineBorder(Color.BLACK),new EmptyBorder(5,5,5,5)));
            setContentAreaFilled(false);
            setOpaque(true);
        }

它适用于我:)

0 个答案:

没有答案