|
|
奔跑的创口贴 · 无需添加引号使PostgreSQL标识符区分 ...· 2 月前 · |
|
|
刚毅的馒头 · 5月反腐报告出炉!86人被调查、48人被处分 ...· 2 年前 · |
|
|
俊秀的勺子 · 苹果电脑怎么升级macOS 10.12 ...· 2 年前 · |
|
|
聪明的手电筒 · 新款BMW 3系亮相,内饰及外观比老_懂车帝· 3 年前 · |
|
|
逃跑的槟榔 · 【ARCFOX极狐阿尔法S口碑】ARCFOX ...· 3 年前 · |
|
|
严肃的荒野 · 金球奖遗珠(六):冰王子出道即巅峰,足坛艺术 ...· 3 年前 · |
| cpu时间 cpu参数 线程池 大小写 |
| https://cloud.tencent.com/developer/information/%E5%A6%82%E4%BD%95%E8%AE%BE%E7%BD%AEJTextArea%E7%9A%84%E5%A4%A7%E5%B0%8F%EF%BC%9F |
|
|
直爽的小蝌蚪
1 年前 |
在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的大小,并根据具体需求选择合适的方法。
|
|
聪明的手电筒 · 新款BMW 3系亮相,内饰及外观比老_懂车帝 3 年前 |