java - 调整 JTextArea 的大小

标签 java swing jpanel jtextarea

我想做的事情非常简单,但我找不到解决方案。

我在 JPanel 中添加了一个 JTextArea,并且我希望在调整 JPanel 大小时调整 JTexArea 的大小。当我调整 JFrame 大小时,我能够调整 JPanel 的大小,但它对于 JTextArea 的工作方式似乎不同。有任何想法吗?这是我使用的代码。

    public HelpPanel( Map<ComponentType, String> compMap ) {
    super();
    this.componentMap = compMap;
    compDescription = new JTextArea();
    setSize(300, 300);

    JScrollPane scroll = new JScrollPane(compDescription);
    add( scroll );
    compDescription.setPreferredSize(new Dimension(getSize()));
    compDescription.setLineWrap(true); //Wrap the text when it reaches the end of the TextArea.
    compDescription.setWrapStyleWord(true); //Wrap at every word rather than every letter.
    compDescription.setEditable(false); //Text cannot be edited.
    displayDescription();
}

最佳答案

super(); 更改为 super(new BorderLayout());

JPanel 默认使用 FlowLayout,它不会尝试强制其组件的大小来填充空间。通过切换到 BorderLayout,中心组件(滚动 Pane )将被迫匹配面板的大小。

关于java - 调整 JTextArea 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21813660/

相关文章:

java - 获取示例程序或教程以备份 android 中的所有内容?

java - 图像图标未加载

java - 为什么我的 textArea 不能实时工作?

java - 从 ActionListener 调用的方法获取返回值

Java:带有 Callables 的 ExecutorService:invokeAll() 和 future.get() - 结果顺序正确吗?

java - Android 中的嵌套 ArrayList

java - 如何在java中的JFrame中排列按钮?

java - 使用 JScrollPane 和 JPanel (自定义)滚动某个矩形的简单方法

Java JPanel 绘画不工作

java - jetty 配置: what is "lowThreads"?