当 JDialog 靠近另一个 JDialog 时,使 JDialog 粘住或吸附到另一个 JDialog
我正在尝试重现我在多个应用程序上看到的功能: 我有一个带有几个 JDialog 的 GUI 应用程序。 我想轻松地将它们紧密地组织在屏幕上: 当我移动一个 JDialog,并且其边框之一“靠近”(例如 5 像素内)到另一个 JDialog 时,我希望它能够自动捕捉并沿着它粘住。
知道如何实现这一目标吗?
I'm trying to reproduce a feature I've seen on several apps:
I have a GUI app with several JDialogs.
I'd like to easily organize them tightly on screen:
when I move one JDialog, and one of its borders gets "close" (within 5 pixels for example) to another JDialog, I'd like it to automatically snap and stick right along it.
any idea how to achieve that ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将 ComponentListener 添加到对话框并侦听 comopnentMoved() 事件。
您可以使用 Window.getWindows() 方法来获取所有 Windows。然后循环遍历窗口并获取每个窗口的边界。每当您靠近窗户时,您都可以手动设置要移动的窗户的大小。
当然,当您想要将窗口从另一个窗口移开时,您还需要处理这种情况,因此也许您需要为每个 componentMoved 事件启动一个计时器,并且仅在事件停止生成后手动定位窗口。
Add a ComponentListener to the dialog and listen for the comopnentMoved() event.
You can use the Window.getWindows() method to get all the Windows. Then you loop through the Windows and get the bounds of each window. Whenever you are near a window you manually set the size of the window you are moving.
Of course you will also need to handle the situation when you want to move the window away from another window so maybe you need to start a Timer with every componentMoved event and only manually position the window after events have stopped being generated.