似乎唯一的选择是设置行数,但我需要我的文本区域可以为用户调整大小。JScrollPane 有帮助,但是当有很多文本时,我想让用户调整区域本身的大小。
我该怎么做呢?如果我可以为此目的使用另一个类,我会完全同意的。
简化的代码是
import javax.swing.*;
import java.awt.*;
public class Problematic {
public static void main(String[] args) {
JFrame f = new JFrame("frame");
f.setLayout(new BorderLayout());
JPanel p1 = new JPanel();
JPanel p = new JPanel();
JButton button = new JButton("Whatever here");
JTextArea t2 = new JTextArea(5, 30);
JScrollPane scrollPane = new JScrollPane(t2);
scrollPane.setSize(600, 400);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
t2.setText("this is some random text\nthat may go for many rows\nso it may look messy");
p1.add(button);
p.add(scrollPane);
f.add(p, BorderLayout.NORTH);
f.add(p1, BorderLayout.CENTER);
f.setSize(600, 500);
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
拉丁的传说
慕的地6264312
慕婉清6462132
慕的地10843
随时随地看视频慕课网APP
相关分类