2

当我将 ActionListeners 与 Drag'n'Drop 结合使用时,我遇到了意外行为。

运行以下示例并尝试以下操作:

  1. 将按钮A拖到某处并释放鼠标按钮
  2. 不要再次将鼠标移到按钮 A 上
  3. 点击按钮B
  4. 在 System.out 上,您将看到按钮AB都会调用 actionPerformed

我不想为按钮A调用 actionPerformed 。我究竟做错了什么?

这是完整的示例:

import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.dnd.*;
import java.awt.event.*;
import java.io.IOException;
import javax.swing.*;

public class DragDropTest{

private static Color slightlyRed = new Color(255, 220, 220);
private static Color slightlyGreen = new Color(220, 255, 220);

private static class DragDropButton extends JButton implements DragSourceListener, DragGestureListener, Transferable, ActionListener{
    private DragSource dragSource;

    public DragDropButton(String string) {
        super(string);
        addActionListener(this);
        dragSource = new DragSource();
        dragSource.createDefaultDragGestureRecognizer(this, DnDConstants.ACTION_COPY, this);
    }

    // Interface DragSourceListener
    @Override public void dragEnter(DragSourceDragEvent dsde) {}
    @Override public void dragOver(DragSourceDragEvent dsde) {}
    @Override public void dropActionChanged(DragSourceDragEvent dsde) {}
    @Override public void dragExit(DragSourceEvent dse) {}
    @Override public void dragDropEnd(DragSourceDropEvent dsde) {
        System.out.println("Dragging of [" + getText() + "] ended");
    }
    // Interface DragGestureListener
    @Override public void dragGestureRecognized(DragGestureEvent dge) {
        dragSource.startDrag(dge, DragSource.DefaultMoveDrop, this, this);
    }
    // Interface Transferable
    @Override public DataFlavor[] getTransferDataFlavors() {return null;}
    @Override public boolean isDataFlavorSupported(DataFlavor flavor) {return false;}
    @Override public Object getTransferData(DataFlavor flavor)
            throws UnsupportedFlavorException, IOException {return null;}
    // Interface ActionListener
    @Override public void actionPerformed(ActionEvent e) {
        System.out.println("Action on [" + getText() + "]");
    }
}

private static class DropPanel extends JPanel implements DropTargetListener{

    public DropPanel(){
        setBorder(BorderFactory.createLineBorder(Color.BLACK));
        setPreferredSize(new Dimension(300, 300));
        setBackground(slightlyGreen);

        new DropTarget(this, this);
    }

    @Override public void dragEnter(DropTargetDragEvent dtde) {}
    @Override public void dragOver(DropTargetDragEvent dtde) {}
    @Override public void dropActionChanged(DropTargetDragEvent dtde) {}
    @Override public void dragExit(DropTargetEvent dte) {}
    @Override public void drop(DropTargetDropEvent dtde) {
        System.out.println("Drop complete");
        dtde.dropComplete(true);
    }

}



// Create Panels

private static JPanel leftSide(){
    JPanel leftPanel = new JPanel();
    leftPanel.setPreferredSize(new Dimension(300, 300));
    leftPanel.setBackground(slightlyRed);
    leftPanel.add(new DragDropButton("A"));
    leftPanel.add(new DragDropButton("B"));
    return leftPanel;
}
private static JPanel rightSide(){
    JPanel leftPanel = new DropPanel();
    return leftPanel;
}

// Create Window

private static JFrame boringFrame(String text){

    JFrame boringFrame = new JFrame(text);
    boringFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    boringFrame.getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER));

    boringFrame.setVisible(true);
    return boringFrame;
}

public static void main(String[] args){

    JFrame boringFrame = boringFrame("boring");

    boringFrame.add(leftSide());
    boringFrame.add(rightSide());   // try to comment out this line!
    boringFrame.pack();


}

}

我的项目中还有一些问题,但我不会打扰你,因为这似乎是我问题的主要来源,但这是我的另一个观察:

如果没有 DropTargetListener (如我的DropPanel),问题就会消失。尝试注释掉以下行:

无聊的Frame.add(rightSide());

4

1 回答 1

3

问题是按钮单击的鼠标释放处理和拖动处理之间的冲突。您可以通过调用getModel().setArmed(false)您的dragGestureRecognized(). 这告诉按钮在下次按下属性设置为 false 时不要触发操作。

缺点是按钮点击并不意味着拖动,但无论如何都会触发拖动,因为用户稍微移动鼠标,不会导致按钮点击。另一方面,对于可拖动按钮,无论如何用户的意图在这种情况下是相当不清楚的。

于 2013-07-05T09:54:27.480 回答