java jtextarea的用法
时间: 2023-11-22 22:05:33 浏览: 106
JTextArea 是 Java Swing 组件库中的一个文本区域组件,可以用来显示多行文本。
下面是 JTextArea 的使用示例:
```java
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class JTextAreaDemo {
public static void main(String[] args) {
JFrame frame = new JFrame("JTextArea Demo");
JPanel panel = new JPanel();
JTextArea textArea = new JTextArea(20, 40); // 创建一个 20 行 40 列的文本区域
JScrollPane scrollPane = new JScrollPane(textArea); // 添加滚动条
panel.add(scrollPane);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
// 设置文本内容
textArea.setText("Hello, world!");
}
}
```
上面的代码演示了如何创建 JTextArea,并将它添加到窗口中。可以通过 `setText()` 方法设置文本内容。JTextArea 默认是自动换行的,可以使用 `setLineWrap()` 方法来禁用自动换行。
阅读全文
相关推荐















