我想做的事情非常简单,但我找不到解决方案。
我在 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/