为什么这个 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
的问题。然而,我的实际应用需要使用它。
编辑:
这是抛出错误时框架的样子:
最佳答案
看看 source code ,这是因为它在进行布局时对组件的大小进行了更正。如果它进行了超过 count * 8 + 10 次更正,那么它会将代码短路以防止无限循环。
相关来源(删除了一些内容)是:
do {
doAgain = false;
for (Iterator<Cell> it = grid.values().iterator(); it.hasNext();) {
ArrayList<CompWrap> 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(如果在进行校正以满足大小依赖性时任何组件更改大小,它就会返回 true)然后它将重复循环,再次进行校正。您看到的是它重复多次时打印的警告消息。由于更正会导致链接组件的大小发生变化,因此您可能会遇到这样一种情况:更正会取消设置一个组件的 y 值并为另一个组件设置 y 值,然后当第一个组件具有其 y 值时设置,它取消设置另一个的 y 值,并重复此过程,直到我们用完重试次数。
Windows L&F 对我来说经常导致这个问题,因为似乎组件总是会陷入这样一种情况,即它们会进行这种校正并且只改变 1 个像素进行校正,但这种校正导致它需要重做另一个组件的布局,导致它向后移动 1 个像素。 “递归”(如果您想这样想的话)是不稳定的,并且没有得出稳定的解决方案。
我不知道删除这些消息的解决方案是什么,但如果它不会在您的应用程序中引起异常的“抖动”(如果是的话您会明白我的意思),我不会担心。这只是一条消息,表明它正在放弃更正,因为它递归了太多次。
关于java - MigLayout 错误 : "Unstable cyclic dependency in absolute linked values!",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26415006/