【问题标题】:How to know the height of the text contained in JTextArea?如何知道 JTextArea 中包含的文本的高度?
【发布时间】:2021-09-01 11:25:48
【问题描述】:

我已经使用过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);

【问题讨论】:

  • 能否提供您使用的代码。这个主题的问题通常发生在你 getSize() 之前这些有实际大小,例如你在它渲染之前调用 getSize 或调用 pack() 或在根容器上设置可见。另一个原因是大小会随您使用的布局而变化。
  • @ntprintf 完成..
  • 不要使用空布局或 AbsoluteLayout。 Swing 旨在与布局管理器一起使用。发布 minimal reproducible example 来展示您的问题。
  • (1-) 这个问题无法回答,因为 OP 没有提供问题的完整上下文。有关完整上下文,请参阅 OP 的最后一个问题:stackoverflow.com/questions/68003700/…
  • @camickr 啊,对不起,我是新来的……感谢您指出最佳的发帖方式。也感谢您的回答,这有助于我今天找到答案。我已经再次更改了我的布局管理器,而不是 null 或 absolute,而是使用框布局。为了获得 jTextarea 的高度,我手动计算它,因为我的宽度是恒定值,所以我可以使用下面 George 的逻辑来计算它。

标签: java swing jtextarea


【解决方案1】:

(如果我理解你的问题)

只需将 textArea 上存在的行数和每行的高度相乘即可:

一个例子:

public class ProductsFrame extends JFrame {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(ProductsFrame::runExample);
    }

    private static void runExample() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JTextArea textArea = new JTextArea(10, 10);

        frame.setLayout(new BorderLayout());

        frame.add(new JScrollPane(textArea));

        textArea.addCaretListener(e -> {
            int linesWithText = textArea.getLineCount();
            int heightOfEachLine = textArea.getFontMetrics(textArea.getFont()).getHeight();

            int heightOfText = linesWithText * heightOfEachLine;

            System.out.println("TEXt HEIGHT:" + heightOfText);
            System.out.println("TEXT AREA HEIGHT:" + textArea.getSize().height);
        });

        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }
}

【讨论】:

  • 不幸的是,OP 还没有学会如何用解决问题所需的所有必要信息提出问题。查看 OP 上一个问题的上下文:stackoverflow.com/questions/68003700/…。这是行不通的,因为在组件实际显示之前不会进行换行,所以在此之前我们不会知道实际的行数是多少。一旦文本区域可见,我们就可以使用 getPreferredSize() 方法。
  • @camickr 好吧,我不知道 OP 什么时候想要得到高度。如果 OP 在 textarea 可见时想要高度,我的答案有效。但这是我的假设。我会等待OP的反馈。如果它对他不起作用,我将删除我的答案。另外,如果 OP 使用null 布局,我认为他/她在放置它时有问题,这意味着 textArea 不可见,这意味着我的答案将不起作用。
  • @GeorgeZ。谢谢你的回答,我可以找到我的逻辑如何从你的回答中计算高度。
猜你喜欢
  • 2022-11-16
  • 2013-07-22
  • 2011-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多