2012-07-09 4 views
4
여기에 언급 한 바와 같이

: Adding Components to the Content Pane,의 LayoutManager의 JFrame의의의 contentPane

기본 콘텐츠 창은 그 레이아웃 관리자로 의 BorderLayout을 JComponent의에서 상속 등을 사용하는 간단한 중간 컨테이너입니다. 그러나

JFrame frame = new JFrame(); 
LayoutManager m = frame.getContentPane().getLayout(); 
System.out.println(m instanceof BorderLayout); // prints true 

, 당신은 다음 코드의 출력을 설명 할 수있다 : 여기

는 증거?

JFrame frame = new JFrame(); 

LayoutManager m = frame.getContentPane().getLayout(); 
System.out.println(m); 
System.out.println(m.getClass().getName()); 

LayoutManager m2 = new BorderLayout(); 
System.out.println(m2); 
System.out.println(m2.getClass().getName()); 

출력 :

javax.swing.JRootPane$1[hgap=0,vgap=0] 
javax.swing.JRootPane$1 
java.awt.BorderLayout[hgap=0,vgap=0] 
java.awt.BorderLayout 
+2

재미있는 질문 – mKorbel

+1

의심 스럽다면, 소스를 읽으십시오 : – kleopatra

답변

5

이 당신의 결과를 설명합니다

protected Container createContentPane() { 
     JComponent c = new JPanel(); 
     c.setName(this.getName()+".contentPane"); 
     c.setLayout(new BorderLayout() { 
      /* This BorderLayout subclass maps a null constraint to CENTER. 
      * Although the reference BorderLayout also does this, some VMs 
      * throw an IllegalArgumentException. 
      */ 
      public void addLayoutComponent(Component comp, Object constraints) { 
       if (constraints == null) { 
        constraints = BorderLayout.CENTER; 
       } 
       super.addLayoutComponent(comp, constraints); 
      } 
     }); 
     return c; 
    } 

의 contentPane를 만드는 방법은 BorderLayout를 상속 익명의 내부 클래스를 만듭니다. 따라서 instanceof에 대한 테스트는 true를 반환하지만 다른 클래스는 반환하므로 클래스 이름이 다릅니다.

+4

또한 클래스 이름의'$'는 내부 클래스가 재생 중임을 알리는 표지입니다. 이 경우 내부 클래스는 익명이므로 실제 이름 대신 $ 1이 표시됩니다. – DPlusV

관련 문제