开发者

JPanel expanded to JScrollPane size when smaller?

开发者 https://www.devze.com 2023-02-10 06:38 出处:网络
So I am trying to have a JPanel inside a JScrollPane, which is located directly inside a JFrame, resize so that it is sometimes smaller than the JScrollPane. Problem is that when I try this it, the le

So I am trying to have a JPanel inside a JScrollPane, which is located directly inside a JFrame, resize so that it is sometimes smaller than the JScrollPane. Problem is that when I try this it, the lesser size seems to be resized to the 开发者_JS百科original size of the JScrollPane. It works fine when I leave it at a larger size. How do I get this to work? Code will be provided if needed.


Wrap you panel in another panel that uses a FlowLayout. This way the outer panel will be resized to fill the viewport of the scroll pane:

JPanel red = new JPanel();
red.setBackground(Color.RED);
red.setPreferredSize( new Dimension(200, 200) );

JPanel outer = new JPanel();
outer.add( red );
add( new JScrollPane(outer) );
0

精彩评论

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