debuglayout.java

来自「java 完全探索的随书源码」· Java 代码 · 共 74 行

JAVA
74
字号
// DebugLayout.java

import java.awt.*;

class DebugLayout implements LayoutManager
{
   public void addLayoutComponent (String name, Component comp)
   {
      System.out.println ("addLayoutComponent called");
      System.out.println ("name = " + name);
      System.out.println ("comp = " + comp);
   }

   public void removeLayoutComponent (Component comp)
   {
      System.out.println ("removeLayoutComponent called");
      System.out.println ("comp = " + comp);
   }

   public Dimension preferredLayoutSize (Container parent)
   {
      System.out.println ("preferredLayoutSize called");
      System.out.println ("parent = " + parent);
      return parent.getSize ();
   }

   public Dimension minimumLayoutSize (Container parent)
   {
      System.out.println ("minimumLayoutSize called");
      System.out.println ("parent = " + parent);
      return parent.getSize ();
   }

   public void layoutContainer (Container parent)
   {
      System.out.println ("layoutContainer called");
      System.out.println ("parent = " + parent);

      Component [] c = parent.getComponents ();

      if (c.length == 0)
          return;

      Dimension size = parent.getSize ();
      Insets in = parent.getInsets ();

      // Compute actual width and height by subtracting parent's
      // insets.

      int width = size.width - in.left - in.right;
      int height = size.height - in.top - in.bottom;

      // Each component will be laid out as a square.

      int cxy = (int) (Math.sqrt (width * height / c.length));

      int x = in.left;
      int y = in.top;

      for (int i = 0; i < c.length; i++)
      {
           if (c [i].isVisible ())
               c [i].setBounds (x, y, cxy, cxy);

           x += cxy;
           if (x >= width)
           {
               x = in.left;
               y += cxy;
           }
      }
   }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?