解决了,我自己找到的答案,可惜不能给自己加分!
寄件者:Phillip Lord ([email protected])
主旨:Re: Scroll a JDesktopPane 
新闻群组:comp.lang.java.programmer
View: Complete Thread (4 articles) | Original Format 
日期:1999/06/16 
 >>>>> "Jordi" == Jordi Guitart <[email protected]> writes:  Jordi> I have a JDesktopPane which contains several JInternalFrames,
  Jordi> and I'd like that this JDesktopPane scrolls when any
  Jordi> JInternalFrame goes out of the screen. I've tried to add the
  Jordi> JDesktopPane to a JScrollPane, but it doesn't work.  Jordi> Thanks.  Jordi> JORDI
 Several people have reported this as a bug to Sun. Im
not sure whether they are going to sort it out or not but I would
think they will in time. Till they do here is my solution, which you
are welcome to use. It still doesnt work perfectly (if a new frame is
produced with bounds which lie partly outside the DesktopPane
scrollbars do appear until something has been moved, or resized), but
its okay.  Anyway use this instead of JDesktopPane, and stick it inside
of a JScrollPane, and all should work. 
import javax.swing.*;
import java.awt.Dimension;
import java.util.Hashtable;
import java.awt.*;
import java.awt.event.*;
/**
 * ScrollableDesktopPane.java
 *
 * This class gives a DesktopPane which responds properly when placed within a 
 * scrollpane. It does this by calculating a preferred size which the JDesktopPane
 * doesnt really. 
 *
 * (PENDING)It could do with implementing a little more cleverly. At the moment it 
 * validates a lot when unnecessary. The preferred should probably be calculated in 
 * the ComponentListeners. It should probably also add an InternalFrameListener also
 *
 *
 * Created: Fri Feb 19 15:08:09 1999
 *
 * @author Phillip Lord
 * @version $Id: ScrollableDesktopPane.java,v 1.3 1999-03-18 19:13:15+00 phillip2 Exp phillip2 $
 */public class ScrollableDesktopPane extends JDesktopPane
{  protected Hashtable listeners = new Hashtable();
  
  public ScrollableDesktopPane()
  {
    super();
  }  /**
   * Set the preferred size of the desktop to the right-bottom-corner of the
   * internal-frame with the "largest" right-bottom-corner.
   *
   * @return The preferred desktop dimension.
   */
  private Dimension preferredSizeOfAllFrames()
  {
    JInternalFrame [] array = getAllFrames();
    
    int maxX = 0;
    int maxY = 0;
    for (int i = 0; i < array.length; i++){
      if ( array[ i ].isVisible() ){
   int x = array[i].getX() + array[i].getWidth();
   if (x > maxX) maxX = x;
   int y = array[i].getY() + array[i].getHeight();
   if (y > maxY) maxY = y;
 }
    } return new Dimension(maxX, maxY);
  }
  
  public void paint( Graphics g )
  {
    setPreferredSize( preferredSizeOfAllFrames() );
    super.paint( g );
  }
  
  /**
   * Add an internal-frame to the desktop. Sets a component-listener on it,
   * which resizes the desktop if a frame is resized.
   */
  public void add( Component comp, Object constraints )
  {
    super.add(comp, constraints);
    registerListener( comp );
  }
  
  public Component add( Component comp )
  {
    registerListener( comp );
    return super.add( comp );
  }
  
  public void add( Component comp, Object constraints, int index )
  {
    super.add( comp, constraints, index );
    registerListener( comp );
  }
  
  public Component add( String name, Component comp )
  {
    registerListener( comp );
    return super.add( name, comp );
  }  public void registerListener( Component comp )
  {
    
    ComponentListener listener = new ComponentListener()
    {
      public void componentResized(ComponentEvent e)
      { 
 // Layout the JScrollPane
 ScrollableDesktopPane.this.revalidate();
 ScrollableDesktopPane.this.repaint();
      }
      public void componentMoved(ComponentEvent e)
      {
 componentResized( e );
      }
      public void componentShown(ComponentEvent e) 
      {
 componentResized( e );
      }
      public void componentHidden(ComponentEvent e) 
      {
 componentResized( e );
      }
    };
    listeners.put(comp, listener);
    comp.addComponentListener(listener);
    revalidate();
  }  /**
   * Remove an internal-frame from the desktop. Removes the
   * component-listener and resizes the desktop.
   */
  public void remove(Component comp)
  {
    super.remove(comp);
    validate();
    comp.removeComponentListener((ComponentListener)
                                 listeners.get(comp));
  }
} // ScrollableDesktopPane/*
 * ChangeLog
 * $Log: ScrollableDesktopPane.java,v $
 * Revision 1.3  1999-03-18 19:13:15+00  phillip2
 * Coded in other add methods to catch all possibilities. Previously scroll
 * bars werent appearing instantly on addition of some components
 *
 * Revision 1.2  1999-02-19 15:33:20+00  phillip2
 * Updated documentation
 *
 * Revision 1.1  1999-02-19 15:29:06+00  phillip2
 * Initial revision
 *
 */
--------------------------------------------------------------------------------