将JLabel拖放到另一个JLabel的顶部

时间:2018-06-29 13:37:46

标签: java swing drag-and-drop jpanel jlabel

我有带有两个图像的JPanel作为JLabel。我正在尝试将右侧图像拖放到左侧图像的顶部,放开鼠标后应该留在那里。目前,我能够拖动图像,但它位于放置位置图像的下方。第二个问题是当我在图像上按下鼠标时,该位置与实际图像边界不同。如何获得相对于主面板的确切位置?

这是我的编码。

public class loadLayout extends JPanel{

        @Override
        public boolean isOptimizedDrawingEnabled() {
            return false;
        }

        public loadLayout(){

            setLayout(new GridLayout(0, 2));
            setPreferredSize(new Dimension(600, 400));
            setMaximumSize(new Dimension(600, 400));
            setMinimumSize(new Dimension(600, 400));


            try {

                BufferedImage img = ImageIO.read(new File("test.jpg"));
                JPanel leftP = new JPanel(null);
                JLabel leftL = new JLabel(new ImageIcon(img));
                leftL.setSize(img.getWidth(), img.getHeight());
                leftL.setLocation(10, 10);
                leftP.add(leftL);

                JPanel rightP = new JPanel();
                BufferedImage img2 = ImageIO.read(new File("dragger.png"));
                JLabel rightL = new JLabel(new ImageIcon(img2));
                rightL.setSize(img2.getWidth(), img2.getHeight());

                rightP.add(rightL);

                add(leftP);
                add(rightP);

                MouseAdapter mouseHandler = new MouseAdapter() {
                    private Point delta;

                    @Override
                    public void mousePressed(MouseEvent e) {

                        imagePoint = SwingUtilities.convertPoint(rightL, rightL.getX(), rightL.getY(), loadLayout.this);
                        Point origin = e.getPoint();

                        Rectangle bounds = new Rectangle(imagePoint, new Dimension(rightL.getWidth(), rightL.getHeight()));
                        if (bounds.contains(origin)) {
                            delta = new Point(origin.x - imagePoint.x, origin.y - imagePoint.y);
                        }
                    }

                    @Override
                    public void mouseDragged(MouseEvent e) {
                        if (delta != null) {
                            imagePoint = e.getPoint();
                            imagePoint.translate(-delta.x, -delta.y);
                            rightL.setLocation(imagePoint);
                        }
                    }

                    @Override
                    public void mouseReleased(MouseEvent e) {

                        if(delta != null){
                            rightL.setLocation(delta);
                            revalidate();
                            repaint();
                        }

                        delta = null;                        
                    }
                };

                addMouseListener(mouseHandler);
                addMouseMotionListener(mouseHandler);


            } catch (IOException ex) {
                Logger.getLogger(DragnDropTest1.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

Location to press to drag the image.

How it should be after..

1 个答案:

答案 0 :(得分:0)

  

我有带有两个图像的JPanel作为JLabel

在我看来,您的面板包含2个面板。这些面板均包含一个标签。为什么将每个标签添加到第二个面板?

摆脱“包装”面板,只需将标签直接添加到主面板即可。

  

我正在尝试将右侧图片拖放到左侧图片的顶部,放开鼠标后应该留在那里

Swing根据Z Order绘制组件。将组件添加到面板中时,ZOrder会增加,并且首先绘制具有最高ZOrder的组件,因此基本上是最后绘制了添加到面板中的最后一个组件。

因此,在您的情况下,右侧标签在左侧标签之前绘制。

如果要拖动右侧标签,以便在左侧标签之后绘制它,那么在mousePressed代码中,您可以使用来更改组件的ZOrder。

JPanel parent = e.getComponent().getParent;
parent.setComponentZOrder(e.getComponent(), 0);

现在,将组件拖到面板上时,该组件将最后绘制一次。