cel*_*yer 0 java swing textarea jscrollpane
我正在使用JScrollPane来保存大面积文本的JTextArea.我将TextArea直接添加到JFrame,它工作正常.但我将它添加到滚动窗格并添加滚动窗格,我没有看到textarea.这是我的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);
}
}
Run Code Online (Sandbox Code Playgroud)
假设textPane是a JScrollPane,你永远不应该向它添加组件.
而是使用 JScrollPane#setViewportView(Component)
JScrollPane 由多个组件组成,它们协同工作,为您提供使组件可滚动所需的功能......

JScrollPane有一个JViewport,用于包含要滚动的组件.您需要将组件"应用"到视图中.
仔细查看如何使用滚动窗格获取更多详细信息