java JScrollPane:如何自动调整滚动中包含的 JPanel 的大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5218232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 10:01:21  来源:igfitidea点击:

JScrollPane: How to resize automatically the JPanel contained in the scroll

javaswingresizejpaneljscrollpane

提问by Jason Rogers

I'm working on a little applet that has a list of items in a JScrollPane on the left.

我正在开发一个小小程序,它在左侧的 JScrollPane 中有一个项目列表。

the user will be able to add and remove elements from this list.

用户将能够在此列表中添加和删除元素。

therefore I need to create a scrollable list.

因此我需要创建一个可滚动列表。

this part is easy.

这部分很容易。

I'm usually tempted to do most of the GUI resizing by hand but I read in the doc of the JScrollPane that it is better to let swing handle it rather than manually changing the dimension.

我通常很想手动调整大部分 GUI 大小,但我在 JScrollPane 的文档中读到,最好让 Swing 处理它而不是手动更改尺寸。

the problem is that I keep adding element and the inside panel doesn't change size.

问题是我一直在添加元素,而内部面板没有改变大小。

any ideas?

有任何想法吗?

here is some of the code I'm using: constructor:

这是我正在使用的一些代码:构造函数:

public SideBarView(Dimension d) {

        super(d);
        Dimension d2 = new Dimension(100,300);

        this.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
        //INSIDE VIEW (zone that is being scrolled)
        container = new SideBarContainer(d2);

//      container.setSize(d2);
//      container.setPreferredSize(d2);
//      container.setMaximumSize(d2);
//      container.setMinimumSize(d2);


        scrollPane = new MyJScrollPane(container);

        new ImageLoaderWorker(this,menuButton).execute();

        Dimension d3 = new Dimension(d.width, d.height-d.width);
        scrollPane.setSize(d3);
        scrollPane.setMaximumSize(d3);
        scrollPane.setMinimumSize(d3);
        scrollPane.setPreferredSize(d3);

        add(scrollPane,BorderLayout.PAGE_START);
        setBorder(BorderFactory.createEmptyBorder());
}

Adding to container:

添加到容器:

element = new JPanel();
        Dimension d2 = new Dimension(100,100);

        element.setSize(d2);
        element.setPreferredSize(d2);
        element.setMaximumSize(d2);
        element.setMinimumSize(d2);

        element.setBackground((panelcount++%2==0)?Color.BLUE:Color.RED);
        cointainer.add(element);

any idea what I'm missing ? or should I just resize the container by hand as I go along?

知道我错过了什么吗?或者我应该在进行过程中手动调整容器的大小?

回答by camickr

You need to tell the container to lay out the components. This is done by revalidating the panel:

您需要告诉容器布置组件。这是通过重新验证面板来完成的:

container.add(element); 
container.revalidate();

回答by Thomas

panel.setSize(d2);
panel.setPreferredSize(d2);
panel.setMaximumSize(d2);
panel.setMinimumSize(d2);

This let's no option to resize for swing, since you tell the layout manager to use exactly the size of d2 (100/100).

这让我们无法调整摆动的大小,因为您告诉布局管理器使用 d2 (100/100) 的大小。

Try without setMaximumSizeand setPreferredSize. You might need to invalidate the panel or scrollpane after adding/removing content as well.

尝试不使用setMaximumSizesetPreferredSize。您可能还需要在添加/删除内容后使面板或滚动窗格无效。

Edit:
I think I considered panelto be your containter. Could you provide the setup code for your scrollpane and the container?

编辑:
我想我认为我panel是你的容器。您能否提供滚动窗格和容器的设置代码?

Did you set a layout manager for the container?

您是否为容器设置了布局管理器?

回答by Thomas

Another possibility, if you don't have to do it yourself, could be to use some SwingX component (like TaskPane etc.). Unfortunately, they seem to be in a transition over to java.net and thus many resources are not available as of now. However, there's a basic download page.

另一种可能性,如果您不必自己做,可以使用一些 SwingX 组件(如 TaskPane 等)。不幸的是,他们似乎正在向 java.net 过渡,因此许多资源目前尚不可用。但是,有一个基本的下载页面