0

我有一个加载的图像(imagePanel),它在类的前面添加了一个 mouseListener。

ImagePanel 正在被拖动,但只能拖动到图像的左上角(0,0)。下面的逻辑是我尝试偏移鼠标坐标但它似乎没有效果,我相信可能是 mousePressed() 没有被调用。

class MouseMotionHandler extends MouseMotionAdapter implements MouseListener
{
    int diffX, diffY;


    public void mousePressed(MouseEvent e)
    {
        diffX = e.getX() - imagePanel.getImageX();
        diffY = e.getY() - imagePanel.getImageY();
    }

    public void mouseDragged(MouseEvent e) 
    {                           
        imagePanel.setX(e.getX()-diffX);
        imagePanel.setY(e.getY()-diffY);
        imagePanel.repaint();
    }
    @Override
    public void mouseExited(MouseEvent e) {
    }
    @Override
    public void mouseEntered(MouseEvent e) {
    }
    @Override
    public void mouseReleased(MouseEvent e) {
    }
    @Override
    public void mouseClicked(MouseEvent e) {
    }
}

任何帮助都会很棒。

编辑:已解决

问题是我在哪里初始化了监听器。mousePressed() 是 MouseListener 类的一部分,mouseDragged() 是 MouseMotionListener 类的一部分。所以我需要为每个添加单独的侦听器:

MouseMotionHandler mouseMotionHandler = new MouseMotionHandler();
imagePanel.addMouseListener(mouseMotionHandler);
imagePanel.addMouseMotionListener(mouseMotionHandler);

谢谢,杰克

4

2 回答 2

0

您是否尝试过使用Transfer Handler?我已经成功使用它

JTable 示例

tbItens.setTransferHandler(new TransferHandler(){

    @Override
     protected Transferable createTransferable(JComponent c)
     {
         JTable table= (JTable)c;
         int row = table.getSelectedRow();
         return new StringSelection(String.valueOf(row));
     }

    @Override
    public int getSourceActions(JComponent c)
    {
        return TransferHandler.COPY;
    }



    @Override
    public boolean canImport(TransferHandler.TransferSupport support)
    {
        if(!support.isDrop())
            return false;

        if (!support.isDataFlavorSupported(DataFlavor.stringFlavor))
                return false;

        boolean copySupported = (COPY & support.getSourceDropActions()) == COPY;
            if (copySupported) {
                support.setDropAction(COPY);
                return true;
            }
        return false;
    }

    @Override
    public boolean importData(TransferHandler.TransferSupport support)
    {
        if(!this.canImport(support))
            return false;
        JTable.DropLocation dl = (JTable.DropLocation) support.getDropLocation();
        int row = dl.getRow();


        String data;
        try
        {
            data = (String)support.getTransferable().getTransferData(DataFlavor.stringFlavor);
        }
        catch (Exception e)
        {
            return false;
        }


        if(data == null || data.isEmpty())
            return false;

        //row = para onde vai
        //i é aonde estava
        int i = Integer.valueOf(data);

        ArrayList<Object> lo = new ArrayList<Object>();
        for(int k=0; k< tbItens.getRowCount(); k++)
        {
            Object v = tbItens.getValueAt(k, 0);
            lo.add(v);
        }
        DefaultTableModel dt = (DefaultTableModel) tbItens.getModel();
        while(dt.getRowCount() != 0)
            dt.removeRow(0);

        Object item = lo.remove((int)i);
        lo.add(row, item);

        for(Object o : lo)
            dt.addRow(new Object[]{o});
        return true;
    }

});
于 2013-02-26T11:30:35.093 回答
0

看看这个,它给出了一个使用鼠标运动监听器的例子。

于 2013-02-26T11:41:58.477 回答