开发者

JScrollPane always clears my Panel

开发者 https://www.devze.com 2023-01-17 11:52 出处:网络
Hy.. I have a JPanel, and in this contentPanel I added some other custom panels and give them locations etc. So now I added a JScrollPane to the contentPanel and always when I scroll down it clears my

Hy.. I have a JPanel, and in this contentPanel I added some other custom panels and give them locations etc. So now I added a JScrollPane to the contentPanel and always when I scroll down it clears my contentPanel, but the panels are still there but not visible...

How can I make them visible again?

That's my code to add the Panel into the contentPanel. The x,y,j are some settingsstuff for the location because I have an fixed window.

private void reloadContentPanel() {

    int x = -200, y = 0, j = 1, row = 4;
    EventPanel panel = nu开发者_如何学JAVAll;

    int i;
    for(i=0; i < this.images.size();i++)
    {
        panel = new EventPanel(this.images.get(i).getAbsolutePath(), 
                               this.images.get(i).getName());

        panel.setLocation(x+(j*200), y);
        j++;
        if(i == row) {
            x = -200;
            y += 205;
            j = 1;
            row += 5;
        }
        this.contentPanel.add(panel);
    }
    this.repaint();
}

Thanks


it sounds like you are not using a LayoutManager correctly.

after creating your JFrame (i'm guessing within your constructor) add the following (for example):

this.setLayout(new FlowLayout());

this will certainly not be the best layout manager for what you are trying to do but will stop the add calls from overriding the displayed component.

you will need to read further about LayoutManagers

besides this, it's not really advisable to extend JFrame. It's better practice to treat JFrame as a member of your class just like all the other components.


I have the answer! :)

I use a GridLayout not a FlowLayout, so it's fine and it automatically refreshes the panels =)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号