简体   繁体   English

在两个JPanel之间拖动Jlabel图像

[英]Dragging Jlabel images between two JPanels

To help better understand how we want the program to function here is a list of what we have so far: 为了更好地了解我们希望程序在这里如何运行,列出了到目前为止的内容:

  • 1 JPanel that has a background floor plan image that functions as the “whiteboard” that the user will place objects on. 1个具有背景平面图图像的JPanel,该图像充当用户放置对象的“白板”。

  • 1 JPanel that holds all of the buttons and controls functionality of a selected object in the “whiteboard” panel 1个JPanel,可容纳所有按钮并控制“白板”面板中选定对象的功能

  • 1 JPanel that is placed in a JScrollPanel that holds a list of JLabels with images to be placed on the “whiteboard” panel 1个JPanel放置在一个JScrollPanel中,该JPanel包含一个JLabel列表,其中的图像要放置在“白板”面板上

  • A class of objects that stores all the properties for each type of furniture item that can be placed; 一类对象,用于存储每种可放置家具类型的所有属性; such as x/y cords, image location, price, labor cost, etc 例如x / y线,图像位置,价格,人工成本等

What we would like to happen is the following: 我们希望发生以下情况:

  1. A user “Mouse Presses and Holds” on a JLabel in the scroll list panel (I assume the JLables will need to be made selectable somehow also) 滚动列表面板中的JLabel上的用户“鼠标按下并按住”(我认为还需要使JLables变为可选状态)
  2. The event then takes the name of the JLabel 然后,该事件使用JLabel的名称
  3. Instantiates an object of the item class based on the name 根据名称实例化项目类的对象
  4. Adds that object to an array of items in the “whiteboard” panel 将该对象添加到“白板”面板中的项目数组中
  5. Then loads a copy of a different image based on the location in the object's properties 然后根据对象属性中的位置加载其他图像的副本
  6. Then attaches that image to the mouse cursor 然后将该图像附加到鼠标光标
  7. As long as the user is holding the mouse button down the image will stay attached 只要用户按住鼠标按钮,图像就会保持附着状态
  8. If the user then releases the mouse button inside the “whiteboard” panel area, an event will be triggered to set the object's location to the coordinates of the mouse cursor 如果用户然后释放“白板”面板区域内的鼠标按钮,则会触发一个事件,以将对象的位置设置为鼠标光标的坐标
  9. If the mouse is released anywhere else, the cursor returns to the default windows one, and the JLabel Remains selected 如果将鼠标释放到其他位置,则光标将返回到默认窗口之一,并且“ JLabel保持”被选中

I don't know if it would be simpler to set up a mouse listener that binds an image to the mouse and then places a copy of it onto the whiteboard wherever it is clicked. 我不知道设置一个将图像绑定到鼠标的鼠标侦听器,然后将其副本放置在白板上,是否更简单。

Here is a link to an image of what has been created so far: http://img824.imageshack.us/img824/4083/o50p.png 这是到目前为止已创建的图像的链接: http : //img824.imageshack.us/img824/4083/o50p.png

在此处输入图片说明

Any tips or ideas would be greatly appreciated! 任何提示或想法将不胜感激!

I would have to say use a JToolBar . 我不得不说使用JToolBar I say this because it has built-in drag'n'drop. 我说这是因为它具有内置的拖放功能。 If you wanted, you could do this manually using Mouse Listeners and java2d . 如果需要,可以使用Mouse Listeners和java2d手动执行此操作。 just put the JLabel images inside a JToolBar . 只需将JLabel图像放入JToolBar good luck! 祝好运!

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

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