1

I was hoping someone would be able to help. This seems like it should be a simple problem but for the life of me I can't work it out.

Problem: I am creating a JPanel that is made up of panels containing 5 labels each with ImageIcons. [sounds confusing]

I am then adding this panel to a JScrollPane. But when it is displayed the images are showing and correctly placed but I am unable to scroll down to see the panels that are off the screen.

here is a screenshot: http://img841.imageshack.us/img841/36/screenshot20120510at160.png

Here is the snippet of code I am using to populate the panels and add the JScrollPane.

private void setSeriesViewContainer(){
    container = new BackgroundPanel(backGround, BackgroundPanel.TILED);
    //container.setPreferredSize(new Dimension(650,500));
    container.setLayout(new BoxLayout(container, BoxLayout.PAGE_AXIS));
    FlowLayout flowLayout = new FlowLayout();
    JPanel[] jp = new BackgroundPanel[10];
        for (int i = 0; i < jp.length; i++) {
        jp[i] = new BackgroundPanel(backGround, BackgroundPanel.TILED);
        jp[i].setLayout(flowLayout);
            for (int j = 0; j < 10; j++) {
                jp[i].add(new JLabel(new         ImageIcon(getClass().getResource("/placeHolder.png"))));

            }

    }
        for (int i = 0; i < jp.length; i++) {
        container.add(jp[i]);

    }
public void init(){
seriesViewContainer = new javax.swing.JScrollPane(container);
seriesViewContainer.setBorder(null);
         seriesViewContainer.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
    seriesViewContainer.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    seriesViewContainer.setPreferredSize(new java.awt.Dimension(700, 300));}

I have searched around for the solution but have not come up with one as yet.

4

2 回答 2

2

container.setPreferredSize(new Dimension(x,y));容器的尺寸应大于滚动窗格的尺寸。

从我读过的内容来看,setPreferredSize() 并不是一件好事。问题可能是容器或 jp 的 LayoutManager。

同样的问题:Java Swing: JScrollPane not working

于 2012-05-10T15:52:48.290 回答
0

您是否尝试在每次添加后调用 revalidate() 到 JScrollPane 和/或容器?

于 2012-05-17T23:13:59.007 回答