简体   繁体   English

与JTextPane一起使用时,JScrollPane不显示

[英]JScrollPane doesn't show when using with JTextPane

I am trying to show a scroll bar next to my text pane but I can't find the reason why it doesn't show. 我试图在我的文本窗格旁边显示一个滚动条,但找不到它不显示的原因。

    this.setLayout(null);

    editorPane = new JTextPane();

    size = editorPane.getPreferredSize();
    editorPane.setBounds(17, 12, 533, size.height * 3);
    editorPane.setBackground(Color.BLACK);
    editorPane.setForeground(Color.WHITE);
    //editorPane.setEditable(false);
    console = editorPane.getStyledDocument();

    scrollConsole = new JScrollPane(editorPane);
    scrollConsole.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

    this.add(editorPane);
    this.add(scrollConsole);

Avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. 避免使用null布局,像素完美布局是现代ui设计中的一种幻觉。 There are too many factors which affect the individual size of components, none of which you can control. 有太多因素会影响组件的单个大小,您无法控制。 Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify Swing旨在与布局经理为核心一起工作,舍弃这些问题不会导致问题和问题的终结,您将花费越来越多的时间来尝试纠正

See Why is it frowned upon to use a null layout in SWING? 请参阅为什么在SWING中使用空布局会让人皱眉? for more details... 更多细节...

You have two basic mistakes... 您有两个基本错误...

  1. You've decided to use a null layout, but neglected to set the size of the JScrollPane 您已决定使用null布局,但忽略了设置JScrollPane的大小
  2. You set the JTextPane as the view for the JScrollPane but then add it to the container, along with the JScrollPane . 您将JTextPane设置为JScrollPane的视图,然后将其与JScrollPane一起添加到容器中。 A component can only belong to a single container, by adding it a second time, you've removed it from the JScrollPane 一个组件只能属于一个容器,通过再次添加它,您已经将其从JScrollPane删除了

See How to Use Scroll Panes for more details 有关更多详细信息,请参见如何使用滚动窗格

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

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