简体   繁体   English

JTextArea无法编辑

[英]JTextArea not editable

I came across the following problem. 我遇到以下问题。 I want to have a scrollable JTextArea and create one like that: 我想要一个可滚动的JTextArea并创建一个像这样的:

JScrollPane scrollableTextArea = new JScrollPane();
JTextArea text = new JTextArea();
scrollableTextArea.add(text);

The result is that I have a grey field that I cannot write into. 结果是我有一个无法写入的灰色区域。

If I create the JTextArea like this however it works: 如果我像这样创建JTextArea,它将起作用:

JScrollPane scrollableTextArea = new JScrollPane(new JTextArea());

Where is my mistake that leads to that behaviour? 导致这种行为的我的错误在哪里?

If I create the JTextArea like this however it works: 如果我像这样创建JTextArea,它将起作用:

A JScrollPane uses its own custom layout manager. JScrollPane使用其自己的自定义布局管理器。 The scrollpane contains areas for: 滚动窗格包含以下区域:

  1. horizontal/vertical scrollbars 水平/垂直滚动条
  2. a "Row Header" and "Column Header" “行标题”和“列标题”
  3. components at the top/right and top/left of the scroll pane 滚动窗格的顶部/右侧和顶部/左侧的组件
  4. the "viewport" which is used to contain the component that you want to scroll “视口”,用于包含要滚动的组件

When you use the following: 当您使用以下内容时:

scrollableTextArea.add(text);

This will mess up the scroll pane because the component is added to the scroll pane directly and not the viewport of the scroll pane 这将使滚动窗格混乱,因为该组件是直接添加到滚动窗格而不是滚动窗格的视口中的

When you use: 使用时:

JScrollPane scrollableTextArea = new JScrollPane(new JTextArea(5, 20));

This will create a scroll pane and add the text area to the viewport of the scroll pane. 这将创建一个滚动窗格,并将文本区域添加到滚动窗格的视口。

Read the section from the Swing tutorial on How to Use ScrollPanes for more information on how the scroll pane works. 阅读Swing教程中有关如何使用ScrollPanes的部分, 获取有关滚动窗格如何工作的更多信息。

只需使用text.setEditable(true)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM