释放鼠标后重新排序项目 [英] Reorder items after mouse release

查看:29
本文介绍了释放鼠标后重新排序项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道哪里出了问题,所以我会试着解释一下,你可以在下面的例子中进行测试:
项目 (3 MB):http://ryannaddy.com/downloads/dist.zip

I am not sure what is wrong, so I will attempt to explain it, and you can test it in the following example:
The Project (3 MB): http://ryannaddy.com/downloads/dist.zip

  1. 解压并运行jar文件
  2. 在应用程序中打开 2 个图像文件
  3. 右侧是所有打开图像的列表,您可以拖放图层以最终对主图片的 z 顺序进行排序.
  4. 如果将顶层拖动到底层下方,它会移回顶部(应移至底层下方)
  5. 如果将顶层向下拖动几个像素,它会将顶层移动到底层下方.
  6. 如果向下拖动底层,它会将其移动到顶层上方(应保持在底部)

最后,我想将这些标签拖到任何位置,然后在那里放置标签.

In the end, I would like to drag those labels to any spot and there is where it will place the label.

释放鼠标后执行的代码块以组织标签:

Code block that gets executed after the mouse is released to organize the labels:

public void lblMouseReleased(MouseEvent evt){
    if(dragging){
        int componentCount = layers.getComponentCount() - 1;
        LayerItem[] comps = new LayerItem[componentCount];

        FocusTraversalPolicy focus = layers.getFocusTraversalPolicy();

        Component comp = focus.getFirstComponent(layers);
        for(int i = 0; i < componentCount; i++){
            Component newComp = focus.getComponentAfter(layers, comp);
            comp = newComp;
            comps[i] = (LayerItem)newComp;
            System.out.println(comps[i].layerID);
        }
        Layers.rebuild(layers, comps);
    }
    dragging = false;
}

Layers.rebuild 的代码:

public static void rebuild(JPanel layers, Component[] list){
    for(int i = 0; i < list.length; i++){
        layers.remove(list[i]);
    }
    for(int i = 0; i < list.length; i++){
        list[i].setBounds(0, i * 50 + 30, 198, 50);
        layers.add(list[i]);
    }
    layers.revalidate();
    layers.repaint();
}

这应该是问题的所有相关代码,如果您需要更多,请告诉我.

This should be all the relevant code to the problem, let me know if you need more.

推荐答案

我明白了!我是因为我没有在面板中获得所有需要的物品.我面板中的第一项是焦点设置为 false 的标题,因为它不是分组的一部分,所以我告诉我的代码只在应该为 0+ 时订购 1+ 项

I got it! I it was because I wasn't getting all the items needed in the panel. The first item in my panel is a header with focus set to false, since it isn't apart of the grouping, so I was telling my code to only order items 1+ when it should have been 0+

这篇关于释放鼠标后重新排序项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆