Is it possible to have “movable”/“draggable” components like JButtons, JTextFields in a JFrame?

末鹿安然 提交于 2019-12-07 03:01:29

问题


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).


回答1:


The Component Mover can do this for you.




回答2:


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.



来源:https://stackoverflow.com/questions/5052141/is-it-possible-to-have-movable-draggable-components-like-jbuttons-jtextfiel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!