简体   繁体   English

需要帮助了解一些行约束行为

[英]Need help understanding some row constraints behaviour

In a Java project I am working on, my constraints were originally set to: 在我从事的Java项目中,约束最初设置为:

JPanel panel = new JPanel();
String layoutConstraints = "fill";
String columnConstraints = "[right]rel[grow]";
String rowConstraints = "[]10[]";
panel.setLayout(new MigLayout(layoutConstraints, columnConstraints, rowConstraints));

From what I understand of the documentation the last gap and constraint will be repeated for every row in excess of the specification. 根据我对文档的理解,超出规范的每一行都会重复最后的差距和约束。 (cf. "If there are fewer rows in the format string than there are in the grid cells in that dimension the last gap and row constraint will be used for the extra rows.") (请参阅“如果格式字符串中的行少于该维度中的网格单元中的行,则最后的间隙和行约束将用于多余的行。”)

So for all I understand, this should mean the row constraints would in fact be []10[]10[]10[] and so on. 因此,据我所知,这实际上意味着行约束将为[]10[]10[]10[] ,依此类推。

My problem comes when I have a JTextArea in a JScrollPane with rows set to 3: 当我在JScrollPane中有一个JTextArea且行设置为3时,就会出现问题:

JLabel label = new JLabel("Text area");
JTextArea text = new JTextArea("Test", 3, 40);
JScrollPane scroll = new JScrollPane(text);
panel.add(label);
panel.add(scroll, "grow,wrap");

When used with a row constraint of []10[] is only 1 row high, yet when I change []10[]的行约束一起使用时,只有1行高,但是当我更改时

String rowConstraints = "[]10[]";

to

String rowConstraints = "[]";

it suddenly uses the full 3 rows height that I specified for the JTextArea. 它突然使用了我为JTextArea指定的全部3行高度。

Have I misunderstood how the row constraints work or is there something else at work here that I totally failed to see/understand? 我是否误解了行约束的工作原理,或者还有其他我完全看不到/理解的事情在起作用?

In both cases you are using only one layout row (ie row managed by MigLayout) to which you put JLabel (first column) and JScrollPanel (containing JTextArea , second column). 在这两种情况下,您都只使用一个布局行(即由MigLayout管理的行),将JLabel (第一列)和JScrollPanel (包含JTextArea ,第二列)放入其中。

You can easily visualize your MigLayout rows and columns structure by simply adding "debug" to layout constraints: 您只需将“ debug”添加到布局约束中,即可轻松可视化MigLayout行和列结构:

String layoutConstraints = "fill,debug";

And launching application. 并启动应用程序。 I really recommend using this setting while working on your layouts. 我真的建议在处理布局时使用此设置。

Rows property that you set for JTextArea have nothing to do with layout rows. 您为JTextArea设置的Rows属性与布局行无关。 This property only tells JTextArea component what size it will try to have, as defined in javadocs : 此属性仅告诉JTextArea组件它将尝试具有什么大小,如javadocs中所定义:

java.awt.TextArea has two properties rows and columns that are used to determine the preferred size. java.awt.TextArea具有两个属性行和列,用于确定首选大小。 JTextArea uses these properties to indicate the preferred size of the viewport when placed inside a JScrollPane (...) 当放置在JScrollPane(...)中时,JTextArea使用这些属性指示视口的首选大小。

But JTextArea rows doesn't correspond by any means to rows in container's (JPanel in your case) layout. 但是, JTextArea行与容器(在您的情况下为JPanel)布局中的行完全不对应。

Please, learn more carefully (and experiment!) what exactly JTextArea row property means. 请仔细学习(并尝试一下!) JTextArea row属性的确切含义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM