简体   繁体   English

具有JScrollPane的JTextArea不会在滚动一次后滚动

[英]JTextArea with JScrollPane isn't scrolling once full

I'm trying to make my JTextArea scrollable when it fills up with text but when I add a JScrollPane it just adds a scrollbar that doesn't do anything. 我试图使我的JTextArea填充文本时可滚动,但是当我添加JScrollPane时,它只是添加了一个不执行任何操作的滚动条。 When I add more text than my JTextArea can display it doesn't change and doesn't append any more text. 当我添加的文本多于我的JTextArea可以显示的文本时,它不会改变并且不会追加任何文本。

    Container window = getContentPane();
    window.setLayout(new FlowLayout());

    display = new JTextArea(TEXT_AREA_ROWS, TEXT_AREA_COLUMNS);
    display.setLineWrap(true);
    display.setPreferredSize(TEXT_AREA_DIMENSIONS);
    display.setBackground(TEXT_BG_COLOR);
    display.setForeground(TEXT_COLOR);
    display.setEditable(false);
    display.setFont(TEXT_FONT);
    window.add(display);

    scroll = new JScrollPane(display);
    scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
    scroll.setPreferredSize(display.getPreferredSize());
    window.add(scroll);

Thanks in Advance 提前致谢

EDIT: Realised my mistake, I was setting the preferred size of the textArea instead of the scrollPane. 编辑:意识到我的错误,我正在设置textArea而不是scrollPane的首选大小。 This is solved by removing display.setPreferredSize(TEXT_AREA_DIMENSIONS); 通过删除display.setPreferredSize(TEXT_AREA_DIMENSIONS);可以解决此问题display.setPreferredSize(TEXT_AREA_DIMENSIONS); and adding a scroll.setPreferredSize(new Dimension(width, height)); 并添加一个scroll.setPreferredSize(new Dimension(width, height)); Silly me. 傻我

but if you append text to it with display.append(string) then the text gets added to the bottom which may or not be on screen at the time. 但是,如果您使用display.append(string)将文本追加到其上,则该文本将被添加到底部,该底部可能在当时也可能不在屏幕上。

Well, you didn't state that you were using the append(...) method in your original question. 好吧,您没有声明在原始问题中使用了append(...)方法。 That is why you should always post a proper SSCCE that demonstrates the problem so we don't have to guess what you are doing. 这就是为什么您应该始终发布适当的SSCCE来说明问题的原因,因此我们不必猜测您在做什么。

See Text Area Scrolling for the probable problem and a solution. 有关可能的问题和解决方案,请参见“ 文本区域滚动 ”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM