java - JTextArea getColumns、getRows 返回 0

标签 java swing

我有一个 JTextArea 我想根据列数和行数显示特定的文本行数组;但 getColumns 和 getRows 返回零。以下内容应该可以让您了解我想要实现的目标:

public class Frame extends JFrame implements ComponentListener {
    ...
    this.textArea = new JTextArea();
    this.textArea.setFocusable(false);
    this.textArea.setFont(new Font(Font.MONOSPACED, Font.BOLD, 20));
    this.add(textArea, BorderLayout.CENTER);
    this.editor = new Editor(new File("test"));
    this.textArea.addComponentListener(this);
    ...
    @Override
    public void componentResized(ComponentEvent ce) {
        this.editor.setHeight(this.textArea.getRows());
        this.editor.setWidth(this.textArea.getColumns());
        drawText();
    }

为什么 getRowsgetColumns 返回零?

最佳答案

使用这个构造函数

javax.swing.JTextArea.JTextArea(int rows, int columns)

编辑:

试试这个:

this.editor.setHeight(this.textArea.getHeight());
this.editor.setWidth(this.textArea.getWidth());

关于java - JTextArea getColumns、getRows 返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29265933/

相关文章:

java - 使用 reduce 和 collect 求平均值

java - 距离小于5米的半正矢公式

java - 构建 jar 时.requestFocusInWindow() 不起作用

java - JTextField:当文本字段已满/自动跳过/自动制表符时聚焦下一个组件

java - 如何退出或退出从 Java 中的主 JWindow 创建的从属 JWindow?

java - JTextArea 始终为空?

java - 我自己的模型中添加的 fireInterval 并不总是更新 JList

java - 找不到适合收集/减少的方法 - 无法使用 Stream() 对整数列表求和

java - 放置在 5 个不同 JPanel 中的 Jbutton 数组中的 Action 监听器

java - Java内置的基因组浏览器 : Swing and awt or Swing and Processing?