添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

java.swing.JTextArea怎么设置输出字体大小

76浏览 · 2024-03-26 15:41:00
在Java Swing中,你可以使用`JTextArea`来显示文本,并且可以通过设置字体属性来改变字体大小。具体方法如下: ```java // 创建JTextArea[对象](https://geek.csdn.net/educolumn/04c51611e4b730957464192e0307b82c?spm=1055.2569.3001.10083) JTextArea textArea = new JTextArea(); // 创建Font[对象](https://geek.csdn.net/educolumn/04c51611e4b730957464192e0307b82c?spm=1055.2569.3001.10083),并设置字体大小为16 Font font = new Font("宋体", Font.PLAIN, 16); // 将Font[对象](https://geek.csdn.net/educolumn/04c51611e4b730957464192e0307b82c?spm=1055.2569.3001.10083)设置为JTextArea的字体 textArea.setFont(font); 在上面的例子中,我们首先创建了一个`JTextArea`[对象](https://geek.csdn.net/educolumn/04c51611e4b730957464192e0307b82c?spm=1055.2569.3001.10083),然后创建了一个`Font`[对象](https://geek.csdn.net/educolumn/04c51611e4b730957464192e0307b82c?spm=1055.2569.3001.10083),并设置字体大小为16。最后将`Font`对象设置为`JTextArea`的字体,这样就可以改变`JTextArea`的字体大小了。 需要注意的是,`Font`对象的第二个参数指定字体样式,它是一个整数常量,可以是`Font.PLAIN`(普通样式)、`Font.BOLD`(粗体样式)、`Font.ITALIC`(斜体样式)或者`Font.BOLD|Font.ITALIC`(粗斜体样式)等。如果你不需要设置字体样式,可以将其设置为`Font.PLAIN`。
相关问题
要更改 JTextArea 中文本的颜色,可以使用 JTextArea 的 setForeground(Color color) 方法。以下是一个简单的示例: ```java JTextArea textArea = new JTextArea(); textArea.setForeground(Color.RED); // 设置字体颜色为红色 ```