无法在JScrollPane中看到组件 [英] Can't see components in JScrollPane
问题描述
我正在使用JScrollPane来保存大面积文本的JTextArea。我将TextArea直接添加到JFrame,它工作正常。但我将它添加到滚动窗格并添加滚动窗格,我没有看到textarea。这是我的SSCCE:
public class foo extends JFrame {
// gui elements
JTextArea chatMonitor = new的JTextArea();
JScrollPane textPane = new JScrollPane();
ChatFrame(最终String昵称,最终String登录,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);
}
}
假设 textPane
是一个 JScrollPane
,你永远不应该向它添加组件。
而是使用获取更多详细信息
I'm using a JScrollPane to hold a JTextArea for a large area of text. I add the TextArea directly to the JFrame, it works fine. But I add it to the scrollpane and add the scrollpane, I don't see the textarea. Here's my 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.
Instead use 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
has a JViewport
, which is used to contain the component you want to be scrolled. You need to "apply" the component to the view.
Take a closer look at How to use Scroll Panes for more details
这篇关于无法在JScrollPane中看到组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!