简体   繁体   English

在JScrollPane中看不到组件

[英]Can't see components in JScrollPane

I'm using a JScrollPane to hold a JTextArea for a large area of text. 我正在使用JScrollPane来保存一个大文本区域的JTextArea。 I add the TextArea directly to the JFrame, it works fine. 我直接将TextArea添加到JFrame,它工作正常。 But I add it to the scrollpane and add the scrollpane, I don't see the textarea. 但是我将其添加到滚动窗格并添加滚动窗格,但看不到文本区域。 Here's my SSCCE: 这是我的SSCCE:

public class foo extends JFrame{
    //gui elements
JTextArea chatMonitor = new JTextArea();

JScrollPane textPane = new JScrollPane();

ChatFrame(final String nickname, final String login, final String server, final String channel){
    setSize(500,500);
    chatMonitor.setEditable(false);
    chatMonitor.setVisible(true);
    textPane.add(chatMonitor);
    textPane.setAutoscrolls(true);
    textPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    textPane.setVisible(true);
    add(textPane);
}
}

Assuming textPane is a JScrollPane , you should never be adding components to it. 假设textPaneJScrollPane ,则永远不要向其添加组件。

Instead use JScrollPane#setViewportView(Component) 而是使用JScrollPane#setViewportView(Component)

JScrollPane is made of a number components which work together to provide you the functionality required to make the component scrollable... JScrollPane由许多组件组成,这些组件可以共同为您提供使组件可滚动所需的功能...

在此处输入图片说明

JScrollPane has a JViewport , which is used to contain the component you want to be scrolled. JScrollPane有一个JViewport ,它用于包含要滚动的组件。 You need to "apply" the component to the view. 您需要将组件“应用”到视图。

Take a closer look at How to use Scroll Panes for more details 详细了解如何使用滚动窗格

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

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