2013-03-05 2 views
0

드래그 앤 드롭 알고리즘을 사용하여 구성 요소를 윈도우 내부로 드래그 할 수있게했습니다. I 창을 이동하면윈도우 이동 후 setLocation

I 프레임 윈도우를 이동할 때까지 잘 수행

...

는, 구성 요소의 위치는 I가 클릭 할 때 I 프레임을 이동 거리와 같은 거리로 시프트 그리고 그것을 끌 때.

누구나 아는 이유는 무엇입니까?

코드 샘플 :

public void mousePressed(final MouseEvent e) { 

    if(SwingUtilities.isLeftMouseButton(e)) { 

     origin = panel.getLocationOnScreen(); 

     panel.setLocation(origin.x, origin.y-panel.getHeight()/2); 

     view.add(panel, JLayeredPane.DRAG_LAYER); 
    } 

} 

public void mouseDragged(MouseEvent e) { 

    if(SwingUtilities.isLeftMouseButton(e)) { 

     panel.setLocation(e.getLocationOnScreen().x - panel.getWidth()/2, e.getLocationOnScreen().y - panel.getHeight()/2); 

    } 

} 
+0

나는'e.getLocationOnScreen()가'panel.setLocation'반면, 글로벌 좌표를 제공한다는 기대 (...)'상대 좌표를 사용합니다. 그러나 그것은 왜 그것이 첫번째 장소에서 작동 하는지를 설명하지 않습니다. –

+0

@Heuster, setLocation을 globale 좌표로 변환하는 방법을 알고 있습니까? –

답변

0

마우스 이벤트 가정은, 당신은 상대 좌표 만 사용해야 패널을 드래그해야하는 컨테이너에 의해 발생합니다. 이다

사용은

public void mouseDragged(MouseEvent e) { 
    if(SwingUtilities.isLeftMouseButton(e)) { 
     panel.setLocation(e.getPoint().x - panel.getWidth()/2, e.getPoint().y - panel.getHeight()/2); 
    } 
}