JTextArea如何设置固定大小
时间: 2024-03-22 15:37:23 浏览: 27
要设置JTextArea的固定大小,可以使用setPreferredSize(Dimension)方法来设置首选大小。例如,要将JTextArea的首选大小设置为宽度为200像素,高度为100像素,可以像下面这样编写代码:
```
JTextArea textArea = new JTextArea();
textArea.setPreferredSize(new Dimension(200, 100));
```
这将使JTextArea在显示时始终具有指定的大小。请注意,如果使用布局管理器,则可能会忽略此首选大小,具体取决于布局管理器的实现方式。
相关问题
jtextArea设置大小
要设置JTextArea的大小,可以使用setPreferredSize(Dimension)方法或者setBounds(int,int,int,int)方法。其中,setPreferredSize(Dimension)方法接受一个Dimension对象,该对象包含了JTextArea的宽度和高度。例如,下面的代码设置了JTextArea的大小为400x300像素:
```
JTextArea textArea = new JTextArea();
textArea.setPreferredSize(new Dimension(400, 300));
```
另外,setBounds(int,int,int,int)方法接受4个参数,分别是x、y坐标和宽度、高度。例如,下面的代码设置了JTextArea的位置和大小:
```
JTextArea textArea = new JTextArea();
textArea.setBounds(50, 50, 400, 300);
```
jtextarea设置大小
你可以使用 `setPreferredSize(Dimension preferredSize)` 方法来设置 JTextArea 的大小,例如:
```
JTextArea textArea = new JTextArea();
textArea.setPreferredSize(new Dimension(500, 300));
```
这将设置 JTextArea 的首选大小为 500 像素宽,300 像素高。请注意,这只是 JTextArea 的首选大小,实际大小可能会受到布局管理器和其他因素的影响。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)