gpt4 book ai didi

java - TextArea 布局中的 ScrollPane(null)

转载 作者:行者123 更新时间:2023-11-30 06:10:18 25 4
gpt4 key购买 nike

这是我的部分代码:

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class WindowTest {
JFrame window = new JFrame();
JPanel panel = new JPanel();
JTextArea text = new JTextArea();
JScrollPane scroll = new JScrollPane(text);

private WindowTest() {
createWindow();
}

public void createWindow() {
window.setLayout(null);
window.setVisible(true);
panel.setVisible(true);
text.setBounds(20, 100, 320, 270);
scroll.setVisible(true);
window.add(scroll);
}

public static void main(String[] args) {
new WindowTest().createWindow();
}
}

我想知道如何将滚动条添加到 TextArea“文本”。这是一个数据库应用程序,它将数据字符串发送到 TextArea。我希望应用程序在必要时显示滚动条(垂直或水平)- TextArea 中的字符串太多。我一直在尝试很多事情,但没有任何效果。布局必须为空,因为我手动制作了所有组件并且我不想从头开始设置所有内容(它只是代码的一部分)。

最佳答案

 text.setBounds(20, 100, 320, 270);

不要在 JTextArea 组件上设置边界。它需要变大才能显示整个文本。然后 JScrollPane 将显示 JTextArea 的一部分。


更新:另外,使用合适的布局管理器,不要使用绝对定位。

更正后的代码:

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class WindowTest {
JFrame window = new JFrame();
JTextArea text = new JTextArea();
JScrollPane scroll = new JScrollPane(text);

private WindowTest() {
createWindow();
}

public void createWindow() {
window.setLayout(new BorderLayout());
window.add(scroll, BorderLayout.CENTER);
window.setVisible(true);
}

public static void main(String[] args) {
new WindowTest().createWindow();
}
}

关于java - TextArea 布局中的 ScrollPane(null),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36066550/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com