2009-09-18 3 views

답변

0

가 GridLayout과에서 각 구성 요소에 Border 추가 간주한다.

0

JPanel을 서브 클래스 화하여 paintChildren() 내에 그리드를 그리십시오. 이 패널에 추가하는 모든 구성 요소에는이 격자가 표시됩니다. 예 :

public class DebugPanel extends JPanel { 

    @Override 
    protected void paintChildren(Graphics g) { 
     super.paintChildren(g); 
     g.setColor(Color.GRAY); 
     // dashed stroke 
     BasicStroke stroke = new BasicStroke(1, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER, 1,new float[]{5},0); 
     ((Graphics2D)g).setStroke(stroke);  
     drawComponentGrid(this,g); 
    } 

    /** 
    * Drawn vertical and horizontal guides for a containers child 
    * components 
    */ 
    private static void drawComponentGrid(Container c, Graphics g) { 
     for(int i=0;i<c.getComponentCount();++i) { 
      Rectangle r = c.getComponent(i).getBounds(); 
      int x = r.x; 
      // vertical line at left edge of component 
      g.drawLine(x,0,x,c.getHeight()); 
      x+= r.width; 
      // vertical line at right edge of component 
      g.drawLine(x,0,x,c.getHeight()); 

      // horizontal line at top of component 
      int y = r.y; 
      g.drawLine(0,y,c.getWidth(),y); 
      // horizontal line at bottom of component 
      y+= r.height; 
      g.drawLine(0,y,c.getWidth(),y); 
     } 
    } 
} 
관련 문제