Java JTextArea getColumns,getRows返回0
我有一个Java JTextArea getColumns,getRows返回0,java,swing,Java,Swing,我有一个JTextArea我想根据列数和行数显示特定的文本行数组;但是getColumns和getRows返回零。以下内容应说明我正在努力实现的目标: public class Frame extends JFrame implements ComponentListener { ... this.textArea = new JTextArea(); this.textArea.setFocusable(false); this.textArea.setFont
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();
}
为什么getRows
和getColumns
返回零?使用此构造函数
javax.swing.JTextArea.JTextArea(int rows, int columns)
编辑:
试试这个:
this.editor.setHeight(this.textArea.getHeight());
this.editor.setWidth(this.textArea.getWidth());
@垃圾上帝,我试图获取可用的列和行,而不是设置它们。行/列属性是文本区域用来确定其首选大小的搜索,它们不会更新。你能解释为什么我应该使用这个特定构造函数吗?不是每个人都仔细阅读一个问题的评论。@MotokoKusanagi您是否尝试过我上面建议的另一种方式?