2013-08-25 18 views
0

我使用JDesktopPane并创建多个JInternalFrame对象。当我最小化和最大化其中任何一个时,打开的框架覆盖所有最小化的框架。JDesktopPane - 最小化JInternalFrames

如何使所有最小化帧可见?

+1

我对你的问题的理解是“如何使所有最小化的内部框架可见”,但我觉得这不是你想要做的。你能否阐述更多,可能包括图像来展示你的问题? – Vulcan

+1

除了@Vulcan建议的图片之外,请考虑发布显示问题的[SSCCE](http://sscce.org/)。只放入2个内部框架。 –

+1

对于[示例](http://stackoverflow.com/a/9422246/230513)。 – trashgod

回答

1

默认情况下,内部框架被最大化以占用桌面的所有空间。通过自定义DesktopManager来更改此行为相对容易。例如:

import java.awt.*; 
import javax.swing.*; 
import java.beans.PropertyVetoException; 

class MaximizeDesktopManager extends DefaultDesktopManager 
{ 
    @Override 
    public void maximizeFrame(JInternalFrame f) 
    { 
     if (f.isIcon()) 
     { 
      try 
      { 
       // In turn calls deiconifyFrame in the desktop manager. 
       // That method will handle the maximization of the frame. 
       f.setIcon(false); 
      } 
      catch (PropertyVetoException e2) {} 
     } 
     else 
     { 
      f.setNormalBounds(f.getBounds()); 
//   Rectangle desktopBounds = f.getParent().getBounds(); 
      Rectangle desktopBounds = getDesktopBounds(f); 
      setBoundsForFrame(f, 0, 0, desktopBounds.width, desktopBounds.height); 
     } 

      // Set the maximized frame as selected. 
     try 
     { 
      f.setSelected(true); 
     } 
     catch (PropertyVetoException e2) {} 
    } 

    private Rectangle getDesktopBounds(JInternalFrame frame) 
    { 
     Rectangle desktopBounds = frame.getParent().getBounds(); 

     for (Component c: frame.getParent().getComponents()) 
     { 
      if (c instanceof JInternalFrame.JDesktopIcon) 
      { 
       desktopBounds.height = Math.min(desktopBounds.height, c.getLocation().y); 
      } 
     } 

     return desktopBounds; 
    } 

    private static void createAndShowUI() 
    { 
     JDesktopPane desktop = new JDesktopPane(); 
     desktop.setDesktopManager(new MaximizeDesktopManager()); 

     desktop.add(createInternalFrame(1, Color.RED)); 
     desktop.add(createInternalFrame(2, Color.GREEN)); 
     desktop.add(createInternalFrame(3, Color.BLUE)); 

     JFrame frame = new JFrame("Maximize Desktop Manager"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.add(desktop); 
     frame.setSize(600, 600); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 

     // Activate first internal frame 

     try 
     { 
      JInternalFrame[] frames = desktop.getAllFrames(); 
      frames[0].setSelected(true); 
     } 
     catch (java.beans.PropertyVetoException e) {} 
    } 

    private static JInternalFrame createInternalFrame(int number, Color background) 
    { 
     JInternalFrame internal = 
      new JInternalFrame("Frame" + number, true, true, true, true); 
     internal.setBackground(background); 
     internal.setVisible(true); 
     int location = 50 * number; 
     internal.setBounds(location, location, 300, 300); 
     return internal; 
    } 

    public static void main(String[] args) 
    { 
     EventQueue.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       createAndShowUI(); 
      } 
     }); 
    } 

} 

不幸的是,如果你调整框架,那么内部框架的边界被重新调整为一致,通过桌面窗格的边界。做这个逻辑是在BasicInternalFrameUI:

/** Invoked when a JInternalFrame's parent's size changes. */ 
public void componentResized(ComponentEvent e) { 
    // Get the JInternalFrame's parent container size 
    Rectangle parentNewBounds = ((Component) e.getSource()).getBounds(); 
    JInternalFrame.JDesktopIcon icon = null; 

    if (frame != null) { 
     icon = frame.getDesktopIcon(); 
     // Resize the internal frame if it is maximized and relocate 
     // the associated icon as well. 
     if (frame.isMaximum()) { 
      frame.setBounds(0, 0, parentNewBounds.width, 
       parentNewBounds.height); 
     } 
    } 

这个逻辑是在专用的内部类的UI的发现,所以它不会轻易改变。至少我不知道该怎么做。