int width = l1.getX() + l1.getWidth();
            int height = l1.getX() + l1.getHeight();
            jvp.setPreferredSize(new Dimension(width ,height));

解决方案 »

  1.   

    而且,里面的控件的Size必须大于JScrolledPane的Size
      

  2.   

    JScrollPane和JScrolledPane有什么不一样啊?还有本来某控件在非可视区域,但是拖动滚动条,那个控件也还是不出现,非要把窗口拉大来才能看得到?
      

  3.   

    设置了JViewport.setPreferredSize(new Dimension(width ,height)),
    只要width与height大于最右下的控件,不管怎么拖都行
      

  4.   

    JViewport.setPreferredSize(new Dimension(width ,height))
    不需要放在drag事件中去吗?还是JViewport是自动的处理。你们一般看什么jfc方面的书
      

  5.   

    怎么去找到最右下那个控件呢?如果最右下是个Icon呢?那可怎么办。
      

  6.   

    怎么去找到最右下那个控件呢?如果最右下是个Icon呢?那可怎么办。
      

  7.   

    循环去找啦      private void resetPrerredSize(JComponent container)
          {
                Component c = null;
                Component components[] = container.getComponents();            int nCount = container.getComponentCount();
                int maxWidth = 0, maxHeight = 0;
                int currentWidth = 0, currentHeight = 0;
                for (int i = 0; i < nCount; i++)
                {
                      c = components[i];
                      currentWidth = (int) (c.getLocation().getX() + c.getWidth());
                      currentHeight = (int) (c.getLocation().getY() + c.getHeight());
                      if (currentWidth > maxWidth)
                      {
                            maxWidth = currentWidth;
                      }
                      if (currentHeight > maxHeight)
                      {
                            maxHeight = currentHeight;
                      }
                }
                container.setPreferredSize(new Dimension(maxWidth + 10, maxHeight + 10));
                Container jScrollPane = (Container)jPanel.getParent();
                if (jScrollPane != null)
                      jScrollPane.validate();
          }
      

  8.   

    谢谢阿土仔,你们都这样子做的吗?
    呵呵!我没有多少开发jfc方面的经验。
    以后还望多多请教。
      

  9.   

    那窗口内的组件可以找到,面板上的Icon就认不出来呀,这个我也不知道怎么办。
      

  10.   

    我看了JViewport有下面几个方法返回Dimesion的,不知道能不能直接应用
    getMaximumSize();
    getMinimumSize();
    getViewSize();