Java Swing JTextArea文本区域的实现示例
作者:xietansheng 时间:2023-10-30 13:40:28
1. 概述
官方JavaDocsApi: javax.swing.JTextArea
JTextArea,文本区域。JTextArea 用来编辑多行的文本。JTextArea 除了允许多行编辑外,其他基本用法和 JTextField 基本一致。
JTextArea 常用构造方法:
/**
* 参数说明:
* text: 默认显示的文本
* rows: 默认可见的行数
* columns: 默认可见的列数
*
* 默认由 rows 和 columns 决定首选大小
*/
JTextArea()
JTextArea(String text)
JTextArea(int rows, int columns)
JTextArea(String text, int rows, int columns)
JTextArea 常用方法:
// 是否自动换行,默认为 false
void setLineWrap(boolean wrap)
// 设置自动换行方式。如果为 true,则将在单词边界(空白)处换行; 如果为 false,则将在字符边界处换行。默认为 false。
void setWrapStyleWord(boolean word)
// 获取文本框中的文本
String getText()
// 追加文本到文档末尾
void append(String str)
// 替换部分文本
void replaceRange(String str, int start, int end)
// 设置文本框的 文本、字体 和 字体颜色
void setText(String text)
void setFont(Font font)
void setForeground(Color fg)
// 获取内容的行数(以换行符计算,满行自动换下一行不算增加行数)
int getLineCount()
// 获取指定行(行数从0开始)的行尾(包括换行符)在全文中的偏移量
int getLineEndOffset(int line)
// 获取指定偏移量所在的行数(行数从0开始)
int getLineOfOffset(int offset)
// 设置颜色,分别为: 光标颜色、呈现选中部分的背景颜色、选中部分文本的颜色、不可用时文本的颜色
void setCaretColor(Color c)
void setSelectionColor(Color c)
void setSelectedTextColor(Color c)
void setDisabledTextColor(Color c)
// 设置文本框是否可编辑
void setEditable(boolean b)
/* 下面方法定义在 java.awt.Component 基类中 */
// 判断组件当前是否拥有焦点
boolean isFocusOwner()
// 设置组件是否可用
void setEnabled(boolean b)
JTextArea 复制粘贴相关方法:
// 设置光标开始位置,selectionStart >= 0
void setSelectionStart(int selectionStart)
// 设置光标结束位置,selectionEnd >= selectionStart
void setSelectionEnd(int selectionEnd)
// 复制选中部分文本
void copy()
// 剪切选中部分文本
void cut()
// 粘贴文本到文本框
void paste()
JTextArea 常用 * :
// 添加焦点事件 *
void addFocusListener(FocusListener listener)
// 添加文本框内的 文本改变 *
textField.getDocument().addDocumentListener(DocumentListener listener)
// 添加按键 *
void addKeyListener(KeyListener listener)
PS: JTextArea 在使用时通常把它放到 JScrollPane 容器中来使用,以此来实现内容增多时可水平/垂直滚动的效果。
2. 代码实例
package com.xiets.swing;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Main {
public static void main(String[] args) {
JFrame jf = new JFrame("测试窗口");
jf.setSize(250, 250);
jf.setLocationRelativeTo(null);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
// 创建一个 5 行 10 列的文本区域
final JTextArea textArea = new JTextArea(5, 10);
// 设置自动换行
textArea.setLineWrap(true);
// 添加到内容面板
panel.add(textArea);
// 创建一个提交按钮,点击按钮获取输入文本
JButton btn = new JButton("提交");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("提交: " + textArea.getText());
}
});
panel.add(btn);
jf.setContentPane(panel);
jf.setVisible(true);
}
}
结果展示:
输入内容,点击按钮,查看控制台输出。
来源:https://blog.csdn.net/xietansheng/article/details/74363663
标签:Swing,JTextArea,文本区域
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java实现的不同图片居中剪裁生成同一尺寸缩略图功能示例
2023-08-23 14:53:15
在Android设备上搭建Web服务器的方法
2023-06-23 23:38:36
![](https://img.aspxhome.com/file/2023/9/82419_0s.gif)
Java实现输出数字三角形实例代码
2023-08-25 02:09:51
![](https://img.aspxhome.com/file/2023/4/58304_0s.png)
JAVA8 十大新特性详解
2023-07-02 10:03:27
【Java IO流】字节流和字符流的实例讲解
2023-08-08 20:45:58
![](https://img.aspxhome.com/file/2023/4/58194_0s.png)
jsp+servlet实现简单登录页面功能(附demo)
2023-09-24 11:32:28
![](https://img.aspxhome.com/file/2023/5/58465_0s.png)
Android TextView中文字通过SpannableString设置属性用法示例
2023-07-26 07:11:51
![](https://img.aspxhome.com/file/2023/8/84748_0s.jpg)
浅谈android Fragment横竖屏翻转对重新加载的要求
2023-07-27 21:55:28
使用logback屏蔽一些包的日志
2023-08-08 20:46:20
基于C语言实现静态通讯录的示例代码
2023-07-02 22:07:38
![](https://img.aspxhome.com/file/2023/2/103102_0s.gif)
Opencv光流运动物体追踪详解
2023-06-21 11:55:31
![](https://img.aspxhome.com/file/2023/4/91994_0s.jpg)
MyBatis JdbcType 与Oracle、MySql数据类型对应关系说明
2023-08-23 02:23:06
![](https://img.aspxhome.com/file/2023/6/58276_0s.jpg)
executor包执行器功能
2023-07-26 21:07:36
关于Spring Boot内存泄露排查的记录
2023-06-27 18:46:19
![](https://img.aspxhome.com/file/2023/1/57591_0s.jpg)
jenkins+maven+svn自动部署和发布的详细图文教程
2023-08-24 05:18:07
![](https://img.aspxhome.com/file/2023/2/58442_0s.png)
深入理解C++中public、protected及private用法
2023-07-02 11:30:17
Springboot与vue实现文件导入方法具体介绍
2023-10-31 12:31:37
![](https://img.aspxhome.com/file/2023/8/58738_0s.png)
SpringBoot FreeWorker模板技术解析
2023-08-09 09:14:39
![](https://img.aspxhome.com/file/2023/6/57826_0s.png)
java判断http地址是否连通(示例代码)
2023-08-05 03:24:05
spring webflux自定义netty 参数解析
2023-07-26 18:38:25
![](https://img.aspxhome.com/file/2023/1/57811_0s.jpg)