简体   繁体   English

获取JFrame的实际当前位置

[英]Getting a JFrame's actual current location

I am trying to create a (child) JFrame which slides out from underneath one side of a second (parent) JFrame. 我试图创建一个(子)JFrame,它从第二个(父)JFrame的一侧下方滑出。 The goal is to then have the child follow the parent around when it is moved, and respond to resizing events. 目的是让孩子在移动时跟随父母,并对大小调整事件做出响应。 This is somewhat related to this question . 这与这个问题有些相关。

I have tried using a ComponentListener, but with this method the child only moves once the parent has come to a stop, whereas I would like the child to move as the parent is dragged around the screen. 我尝试使用ComponentListener,但是使用此方法,子级仅在父级停止时移动,而我希望子级在父级拖动到屏幕周围时移动。

Another option I attempted was to start a new refresher thread that continually updated the child's location using getLocation() or getLocationOnScreen(), but the lag was the same as with ComponentListener. 我尝试的另一个选项是启动一个新的刷新线程,该线程使用getLocation()或getLocationOnScreen()不断更新孩子的位置,但是滞后时间与ComponentListener相同。

Is there a way to get the true actual location of a JFrame even in the midst of a drag? 有没有办法即使在拖动过程中也能获得JFrame的真实位置? or if not, is there a way to get the effect of a sheet sliding out from underneath and following the Frame around? 还是如果没有,是否有办法获得纸张从下方滑出并跟随框架的效果?

如果在Mac上不起作用,则还可以在拖动开始时存储初始位置,然后窗口的位置为initialPosition +鼠标移动。

You should use the mouseDragged(MouseEvent e) method of the MouseAdapter class. 您应该使用MouseAdapter类的mouseDragged(MouseEvent e)方法。 As you drag your main window around, you will get many of these mouseDragged events - handle each one by moving the second window around to a position that's relative to the location of the main window. 在拖动主窗口时,将获得许多这些mouseDragged事件-通过将第二个窗口移动到相对于主窗口位置的位置来处理每个事件。

If you see 'lag' or if it doesn't reposition until the main window stops, try forcing Swing to repaint by calling repaint() on the second window after repositioning and/or by putting your repositioning code inside a SwingUtilities.invokeLater(...) block: 如果看到“滞后”或直到主窗口停止才重新定位,请尝试通过在重新定位后在第二个窗口上调用repaint()和/或将重新定位代码放在SwingUtilities.invokeLater(中)来强制Swing重新绘制。 ..)区块:

SwingUtilities.invokeLater(new Runnable()
{
    @Override
    public void run()
    {
        // your logic here
    }
});

Let me know if this helps. 让我知道是否有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM