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) );
精彩评论