【问题标题】:JscrollPane disappears after adding it to another class which extends to JPanelJscrollPane 添加到另一个扩展至 JPanel 的类后消失
【发布时间】:2016-05-25 14:48:05
【问题描述】:

我有一个从 JPanel 扩展而来的 A 类,它包含一个 JScrollpanel。

SearchTapViewImpl extends JPanel implements SearchTapView {

          SearchTapViewImpl(){
                JPanel panel = new JPanel();
                // ... add stuff to this panel

                JScrollPane scrollPane = new JScrollPane(panel)
                //create stuff
                add(anotherPanel, BorderLayout.NORTH);
                add(scrollpanel, BorderLayout.CENTER);
          }
}

现在我有一个控制器类,它也从 JPanel 扩展而来,它只添加了一个 SearchTapViewImpl 的实例

public class SearchTapController extends JPanel{

       view = new SearchTapViewImpl();
       // stuff
       add((Component)view, BorderLayout.NORTH);
}

现在我有一个从 JFrame 扩展并包含 JTabbedPane 的类 问题是当我添加一个包含 SearchTapViewImpl 实例的 SearchTapController 实例时,滚动窗格不可见。 相反,当我将 SearchTapViewImpl 添加到 Jframe 类时,滚动是可见的。 为什么我通过 SearchTapController 添加时不可见?

// NOT WORKING EXAMPLE
public class StartGUI extends JFrame {
    tabbedPane = new JTabbedPane();
    tabbedPane.addTab("Search", new SearchTapController() ); 
}

//  WORKING EXAMPLE
public class StartGUI extends JFrame {
    tabbedPane = new JTabbedPane();
    tabbedPane.addTab("Search", SearchTapViewImpl() ); 
}

【问题讨论】:

    标签: java swing user-interface


    【解决方案1】:
    add((Component)view, BorderLayout.NORTH);
    

    当您将组件添加到 BorderLayout 的“NORTH”时,组件始终显示在其首选高度,因此无需显示 scollbar。

    尝试将其添加到BorderLayout.CENTER

    【讨论】:

    • 另外需要注意的是不需要强制转换为Component
    猜你喜欢
    • 1970-01-01
    • 2014-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多