在Swing中,我创建了一个定制的Document,能够确定并限制TextArea中的行数,无论是被换行还是按下回车键。我希望在FX中找到类似的东西,但是还没有找到。有任何建议来限制允许的行数吗?
编辑:这就是我试图找出TextArea中有多少个换行符的方式。最大的问题是获取确切的宽度以传递给setWrappingWidth,因为TextArea的内容似乎带有一些内边距和边框。
Text helper = new Text();
helper.setText(text);
helper.setFont(getFont());
helper.wrappingWidthProperty().bind(widthBinding);
Font font = getFont();
FontMetrics fontMetrics = Toolkit.getToolkit().getFontLoader().getFontMetrics(font);
int preferredHeight = new Double(helper.getLayoutBounds().getHeight()).intValue();
int lineHeight = new Double(fontMetrics.getMaxAscent() + fontMetrics.getMaxDescent()).intValue();
System.err.println("preferredHeight / lineHeight: " + preferredHeight / lineHeight);
return preferredHeight / lineHeight;
.setPrefRowCount(10);
”不起作用吗?即使“wrapText”设置为true?此外,文本区域会尊重其区域大小,因此它会尽力填充它,除非您使用maxsize或其他方法对其进行限制。 - Elltz