cla*_*l3r 4 java macos swing jframe componentlistener
有没有办法在拖动 JFrame 时检测其位置?问题是,在 MAX OS X 上,当您停止移动鼠标时,窗口的位置会更新。我看到一个计算新位置和设置窗口手动位置的提示。但因此我必须知道我开始拖动时的位置。为了更清楚一点,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() {
}
Run Code Online (Sandbox Code Playgroud)
小智 5
如果您提到的更新仅在窗口停止移动时发生,并且知道开始拖动时的位置确实可以解决问题,那么我会看到一个选项,您将最后一个位置存储在某个变量中并每次更新它当您检测到移动时。
因此,在 JFrame 类中声明一个私有变量:
点originLocation=new Point(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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3799 次 |
| 最近记录: |