gpt4 book ai didi

java - JscrollPane 在将其添加到另一个扩展到 JPanel 的类后消失

转载 作者:行者123 更新时间:2023-11-29 07:34:40 26 4
gpt4 key购买 nike

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

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 扩展的 Controller 类,它只添加了一个 SearchTapViewImpl 实例

public class SearchTapController extends JPanel{

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

现在我有一个从 JFrame 扩展并包含 JTabbedPane 的类问题是当我添加一个包含 SearchTapViewImpl 实例的 SearchTapController 实例时,滚动 Pane 不可见。相反,当我将 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() );
}

enter image description here enter image description here

最佳答案

add((Component)view, BorderLayout.NORTH);

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

尝试将它添加到 BorderLayout.CENTER

关于java - JscrollPane 在将其添加到另一个扩展到 JPanel 的类后消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37440646/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com