是否可以具有“可移动”/“可拖动”功能? JFrame 中的 JButtons、JTextFields 等组件?
基本上,我计划在 JFrame 上放置一些按钮、文本字段、标签等,并且我希望用户可以使用鼠标在该 JFrame 上移动不同的组件。
我已经看到了使用 MouseListeners、子类化 JComponent 代码、DropSource/DropTarget 实现等的各种方法,但我不确定哪种是“推荐”方法(我不需要支持不同框架/应用程序之间的“拖放”这就是大多数例子似乎所做的)。
Basically I plan to place some buttons, textfields, labels, etc. on a JFrame and I would like to make it possible that a user can move the different components around on that JFrame with the mouse.
I have seen various ways with MouseListeners, subclassed JComponent code, DropSource/DropTarget implementations and so on, but I'm unsure which is the "recommended" way (I don't need to support "drag and drop" between different Frames/Applications which is what most examples seem to do).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
组件移动器可以为您完成此操作。
The Component Mover can do this for you.
使用玻璃窗格:
http://download.oracle.com/javase/tutorial/uiswing/components /rootpane.html
它是一个不可见的面板,位于所有其他组件之上。您可以将鼠标侦听器附加到它,然后使用 SwingUtilities.getDeepestComponentAt() 来确定在 GlassPane 下方单击了哪个组件。然后在玻璃窗格上使用 mouseDragged 侦听器,并根据鼠标拖动事件设置组件位置。
您需要将容器的布局设置为“null”,以便组件的 setLocation 起作用。
Use the GlassPane:
http://download.oracle.com/javase/tutorial/uiswing/components/rootpane.html
It's an invisible panel that sits on top of all other components. You can attach mouse listeners to it and then use SwingUtilities.getDeepestComponentAt() to figure out which component was clicked on beneath the GlassPane. Then use a mouseDragged listener on the glasspane and set the component location based on the mouse dragged event.
You will need to set the layout of your container to "null" so the components' setLocation will work.