简体   繁体   中英

Drag and Snap JLabels in Java

I'm trying to implement a drag and snap type of thing with JLabels. Basically, a JPanel launches with JLabels that are in specified boxes around the screen (all the JLabels and Boxes are the same size). Then, I'd like the user to be able to drag a JLabel into a box and have it "snap" into the box; meaning, when their mouse approaches the box, the program knows to snap that JLabel into that box.

I could do this tediously with mouse listener and mouse dragged and do lots of rendering (I've already begun this, and it's not too pretty). But I was wondering if there is something on the web that anyone knows about, or anything built into java, which would help me.

NOTE: THIS IS NOT A QUESTION ABOUT DRAG AND DROP.

Drag'n'Drop is such a specialty area of requirements.

Take a look at the implementation of the TransferHandler API for Swing components. Works really well for the built in components, takes some work to get to work in custom components but is otherwise impossible to extend (I'm looking at you custom cursor & drag graphics).

The problem is, that any toolkit/API/framework will only work within the constraints of it's target design.

You can make life a "little" easier by separating the various elements of the DnD API, registering listeners, event handling etc, so that you can focus on the requirements of the solution a little better, but, at the moment, that's about as far as you might get.

For example, the API I've written probably won't meet your needs, cause I wrote to solve a series of particular problems I was dealing with.

Just MHO

尝试搜索Java“快照到网格”示例,然后进行相应修改以将gui组件配对吗?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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