Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JScrollPane中正在消失的组件_Java_Swing_Jscrollpane - Fatal编程技术网

Java JScrollPane中正在消失的组件

Java JScrollPane中正在消失的组件,java,swing,jscrollpane,Java,Swing,Jscrollpane,我正在尝试在Swing UI项目中添加滚动视图。所以我有一个JPanel(容器),里面有一些JPanel(卡片),我想在JScrollPane中放一个容器: MealListPanel mlp = new MealListPanel(meals); JScrollPane scrollPane = new JScrollPane(); scrollPane.setBounds(10,50,1000,400); scrollPane.setLayout(new ScrollPaneLayou

我正在尝试在Swing UI项目中添加滚动视图。所以我有一个JPanel(容器),里面有一些JPanel(卡片),我想在JScrollPane中放一个容器:

MealListPanel mlp = new MealListPanel(meals);   
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10,50,1000,400);
scrollPane.setLayout(new ScrollPaneLayout());
scrollPane.add(mlp);
this.add(scrollPane);
之后一切正常,但当我尝试滚动时,一个容器消失:
滚动前:


我做错了什么

-------编辑------ MealListView的代码:

` 公共类MealListView扩展了JPanel{

private final List<Meal> meals;
public MealListView(List<Meal> meals) {
    this.meals = meals;
    this.configure();
    this.drawMeals();
}

private void configure() {
    this.setBounds(0, 0, 1000, 700);
    this.setPreferredSize(new Dimension(1000, 700));
    this.setLayout(null);
}

private void drawMeals()
{   
    System.out.println("Drawing meals");
    int daysCount = 2;
    int mealsCount = 3;

    int columnsCount = 0;               
    int rowsCount = 0;

    int viewWidth = 200;
    int viewHeight = 270;

    for(int index = 0;index < meals.size();index++)
    {           
        MealCardView rp = new MealCardView(meals.get(index));
        Rectangle r = new Rectangle((columnsCount * (10+viewWidth)) + 10,
                 (rowsCount * (10+viewHeight)) + 10,
                 viewWidth, viewHeight);
        rp.setBounds(r);            
        this.add(rp);

        columnsCount++;
        if(columnsCount>=mealsCount-1)
        {
            rowsCount++;
            columnsCount=0;             
        }           
    }

}
私人最终清单餐饮;
公共膳食视图(列出膳食){
这是一顿饭;
这是configure();
这个;
}
私有void configure(){
这个.挫折(0,0,1000,700);
此.setPreferredSize(新维度(1000700));
此.setLayout(null);
}
私人膳食
{   
System.out.println(“绘图餐”);
int daysunt=2;
int-mealsCount=3;
int columnsunt=0;
int rowsunt=0;
int viewWidth=200;
int viewHeight=270;
对于(int index=0;index=mealsCount-1)
{
rowsCount++;
columnsCount=0;
}           
}
}
}


我不得不将这里的布局更改为null(它是FlowLayout)。

在的视图端口中添加
MealListPanel
,而不是使用
滚动窗格。添加(mlp)


您也可以使用或
scrollPane.getViewport().add(mlp)

这很有效,但只是一点点。现在只有卡片是不可见的。为什么要使用
setBounds()
setLayout()
?共享与
MealListPanel
相关的代码?好吧。。。我在主JPanel中使用AbsoluteLayout,所以我必须(?)使用
setBounds()
设置滚动窗格的位置
setLayout()
在这里无关紧要-
ScrollPanelLayout
默认设置。我试图将布局设置为null。
我正在主JPanel中使用AbsoluteLayout,
-不要!Swing设计用于布局管理器。
MealListPanel mlp = new MealListPanel(meals);   
JScrollPane scrollPane = new JScrollPane(mlp);