JScrollPane中消失的组件 [英] Disappearing components in JScrollPane

查看:97
本文介绍了JScrollPane中消失的组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的Swing-UI项目中添加滚动视图.所以我有一个带有一些JPanels(卡片)的JPanel(容器),我想在JScrollPane中放置一个容器:

I'm trying to add scroll view in my Swing-UI project. So I've got a JPanel(container) with some JPanels(cards) inside, and I want to put a container in 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);

此后一切正常,但是当我尝试滚动时,一个容器消失了:
滚动之前:

After that everything is ok, but when I try to scroll, a container disappears:
Before scroll:


收件人

我在做什么错了?

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

------- Edit ------ Code of MealListView:

` 公共类MealListView扩展了JPanel {

` public class MealListView extends 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;             
        }           
    }

}

}

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

I had to change layout here to null (it was FlowLayout).

推荐答案

MealListPanel .html"rel =" nofollow> JScrollPane 的查看端口,而不是使用scrollPane.add(mlp);

Add MealListPanel in JScrollPane's view port instead of using scrollPane.add(mlp);

MealListPanel mlp = new MealListPanel(meals);   
JScrollPane scrollPane = new JScrollPane(mlp);

您可以使用 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆