爪哇.拖动&将 ImageIcon 从面板 1 上的 JLabel 拖放到面板 2 上的 JLabel.

Java. Drag amp; Drop ImageIcon from JLabel on panel 1, to JLabel on panel 2. Add Counter function(爪哇.拖动amp;将 ImageIcon 从面板 1 上的 JLabel 拖放到面板 2 上的 JLabel.添加计数器功能)
本文介绍了爪哇.拖动&将 ImageIcon 从面板 1 上的 JLabel 拖放到面板 2 上的 JLabel.添加计数器功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我已经实现了这个非常基本的,在两个 JPanel 之间拖放,但这并不能真正满足我的要求!

I've implemented this very basic, drag and drop between two JPanels, but this doesn't really meet my requirements!

public class test extends JFrame {

{    JPanel mainpanel, storypanel, imageselect;



    public test(){

          mainpanel = new JPanel(new BorderLayout());
          storypanel = new JPanel();
          imageselect = new JPanel();
            MouseListener listener = new MouseAdapter(){
            public void mousePressed(MouseEvent e)
        {
            JComponent c = (JComponent) e.getSource();
            TransferHandler handler = c.getTransferHandler();
            handler.exportAsDrag(c, e, TransferHandler.COPY);
        }
        };

            int j = 0;
            BufferedImage myImages;
            JLabel imgselect = new JLabel();
            try { myImages = ImageIO.read(new File("four.jpg"));
            //myImages[j] = resize(myImages[j]);
            imgselect= new JLabel(new ImageIcon(myImages));
            System.out.println(j);
            imageselect.add(imgselect);
            imgselect.addMouseListener(listener);
            imgselect.setTransferHandler(new TransferHandler("icon"));

            } catch(Exception e) {};

            int i = 0;
            BufferedImage storyimages;
            JLabel storylabel = new JLabel();


            //targetImg = new ImageIcon("lmkpackage/base/TargetImg.jpg");
            try{ storyimages = ImageIO.read(new File("TargetImg.jpg"));
            //storyimages[i] = resize(storyimages[i]);
            storylabel = new JLabel(new ImageIcon(storyimages));
            System.out.println(i);
            storypanel.add(storylabel); 
            storylabel.addMouseListener(listener);
            storylabel.setTransferHandler(new TransferHandler("icon"));
            } catch(Exception e) {};


            mainpanel.add(imageselect, BorderLayout.NORTH);
            mainpanel.add(storypanel, BorderLayout.SOUTH);

            getContentPane().add(mainpanel);



        }

        public static void main(String[] args){

        System.out.println("Application Running");
        JFrame mainframe =  new test();
            mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainframe.setSize(1000,1000);
            mainframe.setVisible(true);

        }

    }

对不起,我在这里不是很清楚.我试图将 ImageIcon 从图像选择面板上的 JLabel 拖到故事情节面板的另一个 JLabel 上.我可以用上面的代码做到这一点.但是当我这样做时,我可以从图像选择面板中拖动一个 ImageIcon,并在同一面板上替换另一个 ImageIcon.我不希望这种情况发生.我也可以从故事情节面板拖动到图像选择面板,这是我不想要的.

Sorry I wasn't very clear here. Im trying to drag an ImageIcon from a JLabel on the imageselect panel, to another JLabel the storyline panel. I can do this, with the above code. But when I do this, I can drag an ImageIcon from the imageselect panel, and replace another ImageIcon on the same panel. I do NOT want this to happen. I can also drag from the storyline panel to the imageselect panel, which I do NOT want.

我不是要求被勺子喂食代码,我只是在寻找正确方向的推动力!

I'm not asking to be spoon fed code, I'm just looking for a push in the right direction!

我想知道是否有任何方法可以计算成功的拖放操作,下面的解决方案回答了我原来的问题.

I'm wondering is there any way of counting a successful drag and drop operation, the solution below answered my original question.

推荐答案

这基本上是@Andrew 评论的大纲 - 由于您的 SSCCE 很容易实现 :-)

This is basically an outline of @Andrew's comment - easily possible due to your SSCCE :-)

子类 TransferHandler,覆盖 canImport 以检查源标签是否在 imageSelect 面板上,如果是则拒绝.

Subclass TransferHandler, override canImport to check if the source label is on the imageSelect panel and reject if so.

    // custom transferHandler which decides about imports based on source
    TransferHandler handler = new TransferHandler("icon") {

        @Override
        public boolean canImport(TransferSupport support) {
            return super.canImport(support) 
                    && support.getComponent().getParent() != imageSelectPanel;
        }

    };
    // use the handler on all labels (handlers can be shared, btw)
    // for each label on imageSelectPanel
    imageSelectLabel.setTransferHandler(handler)
    // for each label on the target panel (aka storyPanel) 
    storyLabel.setTransferHandler(handler)

这篇关于爪哇.拖动&将 ImageIcon 从面板 1 上的 JLabel 拖放到面板 2 上的 JLabel.添加计数器功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)