我已经使用了getPreferredSize()
、getSize()
、getMaximumSize()
和getMinimumSize()
。但是它们都没有给予我JTextArea
中文本的准确高度。
JTextArea txt = new JTextArea();
txt.setColumns(20);
txt.setLineWrap(true);
txt.setRows(1);
txt.setToolTipText("");
txt.setWrapStyleWord(true);
txt.setAutoscrolls(false);
txt.setBorder(null);
txt.setText("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.");
add(txt, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 40, 540, -1));
JOptionPane.ShowMessageDialog(null, txt.getPreferredSize().height);
2条答案
按热度按时间dm7nw8vv1#
我发现
textArea.getPreferredScrollableViewportSize()
非常有帮助-它包含了文本字段在没有滚动的情况下需要完全显示的区域。pkmbmrz72#
(If我理解你的问题)
只需将textArea上存在的行数和每行的高度相乘:
举个例子: