jtextarea固定大小
时间: 2023-07-18 09:15:21 浏览: 60
你可以通过设置JTextArea的大小和边框样式来固定它的大小,如下所示:
```
JTextArea textArea = new JTextArea();
textArea.setPreferredSize(new Dimension(300, 200)); // 设置大小
textArea.setBorder(BorderFactory.createLineBorder(Color.BLACK)); // 设置边框样式
```
这样就可以将JTextArea的大小固定为300x200,并且有一个黑色的边框。
相关问题
JTextArea如何设置固定大小
要设置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);
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)