在保留原始JLabel的同时拖放JLabel

时间:2015-03-20 03:21:38

标签: jlabel

我正在开发一个GUI,它在左侧显示一个平面图的图像(带有ImageIcon的JLabel)和一些小图标(带有ImageIcon的JLabel)。我们的想法是能够选择其中一个图标并将其拖放到平面图上的位置。我的代码工作正常,但是当您将图标拖到平面图上时,我需要保留原始图标,因此如果需要,可以将其放置在平面图上的其他几个位置。因此,我需要能够克隆鼠标按下时移动的图标。

下面的代码显示了// ********标记之间的临时修复,但当然这只适用于最顶层的图标。我需要以某种方式克隆"组件comp"作为一个新的JLabel。

以下是我的代码的相关部分:

class MouseHandler extends MouseAdapter {

    private Component dragComponent;
    private Sidebar board;
    private Point dragOffset;

    public MouseHandler(Sidebar board) {
        this.board = board;
    }

    public Sidebar getBoard() {
        return board;
    }

    @Override
    public void mousePressed(MouseEvent e) {
        Component comp = getBoard().getComponentAt(e.getPoint());            
        if (comp != null) {
            if (comp instanceof JLabel) {                                           

                //**************
                String imagePath = "/Downlight 1.gif";
                Image Images = new ImageIcon(this.getClass().getResource(imagePath)).getImage();
                String path = "Images" + imagePath;
                ImageIcon icon = new ImageIcon(path); 
                JLabel lbl = new JLabel(icon);
                lbl.setBounds(22, 26, 36, 36);
                lbl.setIcon(new ImageIcon(Images));
                lbl.setOpaque(true);
                Sidebar board = getBoard();
                board.add(lbl, new Point(40, 44));
                board.setComponentZOrder(lbl, 0);
                //**************

                dragComponent = comp;
                dragOffset = new Point();
                dragOffset.x = e.getPoint().x - comp.getX();
                dragOffset.y = e.getPoint().y - comp.getY(); 
            }
        }
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        if (dragComponent != null) {
            board = getBoard();
            Point dragPoint = new Point();
            dragPoint.x = e.getPoint().x - dragOffset.x;
            dragPoint.y = e.getPoint().y - dragOffset.y;
            dragComponent.setLocation(dragPoint);
        }
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        if (dragComponent != null) {
            dragComponent = null;
        }
    }
}

}

Thanks in advance for any help with this.   

1 个答案:

答案 0 :(得分:0)

我找到了一个解决方案,使我能够将原始JLabel的图标和边界复制到新的JLabel,它取代了被拖动的JLabel:

    @Override
    public void mousePressed(MouseEvent e) {
        Component comp = getBoard().getComponentAt(e.getPoint());            
        if (comp != null) {
            if (comp instanceof JLabel) {                                           

                JLabel label = (JLabel) comp;
                ImageIcon icon = ((ImageIcon)label.getIcon());
                Rectangle rect = label.getBounds(); 
                JLabel lbl = new JLabel(icon);
                lbl.setBounds(rect);
                lbl.setVisible(true);;
                lbl.setOpaque(true);
                Mainpanel board = getBoard();
                board.add(lbl);
                board.setComponentZOrder(lbl, 0);

                dragComponent = comp;
                dragOffset = new Point();
                dragOffset.x = e.getPoint().x - comp.getX();
                dragOffset.y = e.getPoint().y - comp.getY(); 

            }
        }
    }