将JscrollPane添加到另一个扩展到JPanel的类后,它就会消失

Per*_*ero 1 java user-interface swing

我有一个从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);
          }
}
Run Code Online (Sandbox Code Playgroud)

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

public class SearchTapController extends JPanel{

       view = new SearchTapViewImpl();
       // stuff
       add((Component)view, BorderLayout.NORTH);
}
Run Code Online (Sandbox Code Playgroud)

现在我有一个从JFrame扩展并包含JTabbedPane的类,当我添加一个SearchTapController实例时,它包含一个SearchTapViewImpl实例,滚动窗格不可见.相反,当我将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() ); 
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述 在此输入图像描述

cam*_*ckr 5

add((Component)view, BorderLayout.NORTH);
Run Code Online (Sandbox Code Playgroud)

将组件添加到"NORTH"时BorderLayout,组件始终显示在其首选高度,因此不需要显示scollbar.

尝试将其添加到BorderLayout.CENTER.