2010-11-24 7 views
2

JLayeredPane이 있고 JLayeredPane 내부에 JInternalFrame이 있습니다. 필요한 것은 JInternalFrame의 내용 창 경계 (x 위치, y 위치, 너비, 높이)입니다. 경계는 JLayeredPane에 관련 지을 필요가 있습니다. 내 문제는 테두리, 제목 표시 줄 및 JInternalFrame 다른 내 계산 난장판 모르겠어요. 몇 픽셀 떨어져있어.Java에서 구성 요소의 좌표 가져 오기

어떻게 계산하려고합니까? 이 코드는 JInternalFrame에 있습니다.

 

Rectangle area = new Rectangle(
       getX() + 2, //The x coordinate of the JInternalFrame + 2 for the border 
       getY() + ((javax.swing.plaf.basic.BasicInternalFrameUI) getUI()).getNorthPane().getHeight(), //The Y position of the JinternalFrame + theheight of the title bar of the JInternalFrame 
       getContentPane().getWidth() - 2, //The width of the Jinternals content pane - the border of the frame 
       getContentPane().getHeight() //the height of the JinternalFrames content pane 
       ); 
 

내부 프레임의 내용 창 위치를 가져와야합니다. alt text

답변

6

무엇에 관한 좌표입니까? 화면? 창?

getLocation()을 호출하면 창 계층 구조의 구성 요소 부모와 관련된 좌표가 표시됩니다.

SwingUtilities에는 하나의 참조 프레임에서 다른 좌표 프레임으로 좌표를 변환하는 몇 가지 방법이 있습니다. 따라서 제목 표시 줄과 프레임을 보완하려면 다음을 수행하십시오.

JInternalFrame iframe = ... 
Container c = iframe.getContentPane(); 
Rectangle r = c.getBounds(); 
r = SwingUtilities.convertRectangle(c.getParent(), r, iframe.getParent()); 
+0

매우 근사합니다. 나는 이것을 할 수있는 간단한 방법이 있음을 알고있었습니다. 이것은 완벽하게 작동했습니다. – user489041

2

JInternalFrame의 getBounds() 메서드는 필요한 좌표로 Rectangle을 반환합니다. 다음을 수행하는 것이 가장 좋습니다 :

{ 
     ... 
     JInternalFrame iframe = ...; 
     Rectangle r = new Rectangle(iframe.getBounds()); 
} 

이렇게하면 경계를 수동으로 계산할 필요가 없습니다.

+0

이 문제는 내 프레임의 제목 표시 줄과 테두리를 포함하는 경계를 제공합니다. 내용 창 경계 만 필요합니다. 그러나 나는 x 좌표와 y 좌표가 JLayeredPane의 내용 창 위치에 필요합니다. – user489041