我通过 swing 创建了一个演示,我想在 textarea 中显示全文。我想将两个文本区域添加到滚动条中以显示滚动条中的所有组件。为此,我创建了一个面板,然后在此面板上添加两个 textarea,然后将此面板添加到滚动条中。当我运行演示时,它显示正确,当我调整窗口大小时它也正确显示,但是当我调整小屏幕时它有问题。当屏幕变小(而不是出现水平滚动条)时,它不会换行文本。你能帮我在缩小屏幕时自动换行吗?
这是我的代码: JPanel gui = new JPanel(new GridLayout()); gui.setBorder(new EmptyBorder(2, 3, 2, 3));
JTextArea area = new JTextArea("JTextArea on JPanel inside JScrollPane does not resize properly");
area.setWrapStyleWord(true);
area.setLineWrap(true);
gui.add(area);
JTextArea area1 = new JTextArea("JTextArea on JPanel inside JScrollPane does not resize properly");
area1.setWrapStyleWord(true);
area1.setLineWrap(true);
gui.add(area1);
gui.setBackground(Color.WHITE);
JScrollPane scroller = new JScrollPane(gui);
//scroller.setBorder(new EmptyBorder(2, 3, 2, 3));
JFrame f = new JFrame("Big Text Fields");
f.add(scroller,BorderLayout.CENTER);
// Ensures JVM closes after frame(s) closed and
// all non-daemon threads are finished
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// See http://stackoverflow.com/a/7143398/418556 for demo.
f.setLocationByPlatform(true);
// ensures the frame is the minimum size it needs to be
// in order display the components within it
f.pack();
// should be done last, to avoid flickering, moving,
// resizing artifacts.
f.setVisible(true);