【问题标题】:Can't see components in JScrollPane在 JScrollPane 中看不到组件
【发布时间】:2013-09-25 06:07:46
【问题描述】:

我正在使用 JScrollPane 来保存用于大面积文本的 JTextArea。我将 TextArea 直接添加到 JFrame,它工作正常。但是我将它添加到滚动窗格并添加滚动窗格,我没有看到文本区域。这是我的 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);
}
}

【问题讨论】:

  • 简短,是的。自给自足,没有。正确(可编译),不。示例,很难说这是否算作示例。

标签: java swing textarea jscrollpane


【解决方案1】:

假设textPaneJScrollPane,你永远不应该向它添加组件。

改为使用JScrollPane#setViewportView(Component)

JScrollPane 由多个组件组成,它们协同工作,为您提供使组件可滚动所需的功能...

JScrollPane有一个JViewport,用来包含你想要滚动的组件。您需要将组件“应用”到视图。

仔细查看How to use Scroll Panes了解更多详情

【讨论】:

  • 谢谢!这很有帮助!
  • 在 Swing 容器 API 中,滚动窗格是一个例外,很多人都遇到过这个问题 ;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多