2012-10-11 3 views
2

드래그하는 동안 JFrame의 위치를 ​​감지하는 방법이 있습니까? 문제는 MAX OS X에서 마우스 이동을 멈 추면 윈도우 위치가 업데이트된다는 것입니다. 새로운 위치를 계산하고 창 설명서의 위치를 ​​설정하는 팁을 보았습니다. 그러나 그러므로 내가 끌기 시작했을 때의 위치를 ​​알아야합니다. 좀 더 명확하게하기 위해 JFrame은 화면을 캡처하는 데 사용되지만 주위를 움직이면 이전 위치에 있다고 생각하여 업데이트하지 않습니다. 끌기 이동을 멈 추면 (그러나 마우스 단추를 계속 누르고있을 수 있음) 업데이트합니다.jframe의 드래그 감지

import java.awt.event.ComponentListener; 
import java.awt.Component; 
import java.awt.event.ComponentEvent; 
import javax.swing.JFrame; 

void setup() { 
    frame.addComponentListener(new ComponentListener() 
    { 
    public void componentMoved(ComponentEvent evt) { 
     Component c = (Component)evt.getSource(); 
     println("moved "+frameCount); 
    } 

    public void componentShown(ComponentEvent evt) {} 

    public void componentResized(ComponentEvent evt) {} 

    public void componentHidden(ComponentEvent evt) {} 
    } 
); 
} 

void draw() { 
} 
+1

* ". 문제는 MAX OS X에 그 마우스를 이동 중지 윈도우 업데이트의 위치입니다"* 그리고 정확히 ..why는 점이다 앱에 문제가 있습니까? 여담으로. 1) 앱이있는 경우 화면 캡처를 수행하고 프레임에 드래그되는 시각적 신호가 표시되면 이미지를 분석하여 움직임을 감지 할 수 있습니다. 2) 통신 할 수있는 호환되지 않는 코드는 무엇입니까? –

답변

3

하면 윈도우가 이동을 중지하고, 당신이 정말로 문제를 해결할 수있는 드래그 시작했을 때의 위치를 ​​알고 있다면, 난 당신이 마지막 위치를 저장하는 옵션이 표시 한 경우에만 일어나는 업데이트에 관한 언급 무엇 일부 변수에서 이동을 감지 할 때마다 업데이트합니다.

그래서 당신의 JFrame의 클래스에서 개인 변수를 선언 :

포인트 originLocation = 새로운 포인트 (0, 0);

당신이 할 수있는 리스너 방법에

:

public void componentMoved(ComponentEvent evt) { 
      Component c = (Component)evt.getSource(); 
      Point currentLocationOnScreen=c.getLocationOnScreen(); 

// do your requirements here with the variables currentLocationOnScreen and originLocation 

      // update the originLocation variable for next occurrences of this method 
      originLocation=currentLocationOnScreen; 
    }