MigLayout错误:“绝对链接值中的不稳定循环依赖!”

为什么这个SSCCE(使用MigLayout库)……

public static void main(String[] args) { try { UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) { e.printStackTrace(); } JFrame frame = new JFrame(); frame.setLayout(new MigLayout(new LC().fill().insetsAll("0"))); JTabbedPane jtp = new JTabbedPane(); jtp.add(new JPanel(), "Tab 1"); jtp.add(new JPanel(), "Tab 2"); JLabel label = new JLabel("label"); JPanel panel = new JPanel(new MigLayout(new LC().fill())); panel.add(jtp, "id tabbedpane, grow, span"); panel.add(label, "pos (tabbedpane.w-label.w) 10, id label"); label.setBounds(100, 100, 10, 10); frame.add(panel, "grow, span"); frame.setSize(500, 500); frame.setLocationRelativeTo(null); // Sorry, Andrew Thompson frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } 

抛出此错误:

 Unstable cyclic dependency in absolute linked values! Unstable cyclic dependency in absolute linked values! Unstable cyclic dependency in absolute linked values! Unstable cyclic dependency in absolute linked values! 

我想通了,如果你删除WindowsLookAndFeel代码,那么一切运行正常……

在此处输入图像描述

因此,这是MigLayout和WindowsLookAndFeel的问题。 然而,我的真实应用需要它的使用。

编辑:

这是抛出错误时框架的样子:

在此处输入图像描述

看一下源代码 ,就会发生这种情况,因为它在进行布局时会对组件的大小进行修正。 如果它超过计数* 8 + 10校正,那么它会使代码短路以防止无限循环。

相关来源(删除了一些内容)是:

 do { doAgain = false; for (Iterator it = grid.values().iterator(); it.hasNext();) { ArrayList compWraps = it.next().compWraps; for (int i = 0, iSz = compWraps.size(); i < iSz; i++) { CompWrap cw = compWraps.get(i); if (j == 0) { doAgain |= doAbsoluteCorrections(cw, bounds); // . . . } // . . . } } clearGroupLinkBounds(); if (++count > ((compCount << 3) + 10)) { System.err.println("Unstable cyclic dependency in absolute linked values!"); break; } } while (doAgain); 

所以发生的事情是,如果doAbsoluteCorrections返回true(如果任何组件在完成修正以满足大小依赖性时改变大小,那么它将重复循环,这将再次进行更正)。 你所看到的是它重复这么多次时打印的警告信息。 由于校正可能导致链接组件的大小发生变化,因此您可以获得这样的情况:更正为一个组件设置y值并为另一个组件设置y值,然后当第一个组件具有其y值时设置,它取消设置另一个的y值,这将重复,直到我们用完重试。

Windows L&F经常对我造成这个问题,因为看起来组件总是会遇到这样的情况:他们会进行这种校正并且只能更改1个像素进行校正,但是这种校正导致它需要重做布局另一个组件导致它向后移动1个像素。 “递归”(如果你想这样想)是不稳定的,并没有达到稳定的解决方案。

我不知道删除这些消息的解决方案是什么,但是如果它不会在你的应用程序中引起exception的“摇摆”(你会知道我的意思,如果它),我不会担心它。 这只是一条消息,表明它正在放弃更正,因为它被递归了太多次。