|
|
叛逆的脸盆 · 大熊貓龍鳳胎寶寶命名比賽 | 香港海洋公園· 4 月前 · |
|
|
热心肠的山羊 · Select 查询 - Apache Doris· 5 月前 · |
|
|
细心的仙人球 · lua loadfile - CSDN文库· 1 年前 · |
|
|
面冷心慈的树叶 · 如何使用VBA代码创建一个搜索文件夹以查找消息类?· 1 年前 · |
|
|
伤情的匕首 · 上汽集团2021年净利同比增20.08%,整 ...· 2 年前 · |
在Java Swing中,JTextArea是一个用于显示和编辑多行文本的组件。要设置JTextArea的大小,可以通过以下几种方法:
你可以直接使用
setPreferredSize
方法来设置JTextArea的大小。
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("JTextArea Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
JTextArea textArea = new JTextArea();
textArea.setPreferredSize(new java.awt.Dimension(300, 200));
frame.add(textArea);
frame.setVisible(true);
}
你也可以使用
setBounds
方法来设置JTextArea的位置和大小。
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("JTextArea Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
JTextArea textArea = new JTextArea();
textArea.setBounds(50, 50, 300, 200);
frame.add(textArea);
frame.setLayout(null); // 需要设置布局管理器为null
frame.setVisible(true);
}
你还可以使用布局管理器来自动调整JTextArea的大小。常用的布局管理器有
BorderLayout
、
GridLayout
等。
import javax.swing.JFrame;
import javax.swing.JTextArea;
import java.awt.BorderLayout;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("JTextArea Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
JTextArea textArea = new JTextArea();
frame.add(textArea, BorderLayout.CENTER);
frame.setVisible(true);
}
setPreferredSize
和
setBounds
方法。
JScrollPane
来提供滚动条。
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("JTextArea Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
JTextArea textArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(textArea);
frame.add(scrollPane);
frame.setVisible(true);
}
通过以上方法,你可以灵活地设置JTextArea的大小,并根据具体需求选择合适的方法。
|
|
叛逆的脸盆 · 大熊貓龍鳳胎寶寶命名比賽 | 香港海洋公園 4 月前 |
|
|
热心肠的山羊 · Select 查询 - Apache Doris 5 月前 |
|
|
细心的仙人球 · lua loadfile - CSDN文库 1 年前 |
|
|
面冷心慈的树叶 · 如何使用VBA代码创建一个搜索文件夹以查找消息类? 1 年前 |