gpt4 book ai didi

java - 是否可以使用 java Swing DnD api 在 mousePressed 上开始拖动?

转载 作者:行者123 更新时间:2023-12-02 03:43:17 24 4
gpt4 key购买 nike

在下面的示例中,有一个标准的触发拖放的方法,即 mousePress+mouseMove。

import javax.swing.*;
import java.awt.datatransfer.StringSelection;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DragGestureListener;
import java.awt.dnd.DragSource;

public class DndExample extends JFrame {

public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new DndExample());
}

public DndExample() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JLabel dragLabel = createDndLabel();
getContentPane().add(dragLabel);
pack();
setVisible(true);
}

private JLabel createDndLabel() {
JLabel label = new JLabel("Drag me, please");


DragGestureListener dragGestureListener = (dragTrigger) -> {
dragTrigger.startDrag(null, new StringSelection(label.getText()));
};

DragSource dragSource = DragSource.getDefaultDragSource();
dragSource.createDefaultDragGestureRecognizer(label, DnDConstants.ACTION_COPY, dragGestureListener);

return label;
}
}

是否可以在没有 mouseMove 的情况下在 mousePressed 上触发 startDrag?所需的行为是这样的:我按下鼠标按钮,然后光标发生变化,表明拖动已开始,如果移动鼠标,则继续拖动。我显然知道我可以添加 MouseListener 并手动更改光标,但是需要更多代码来恢复以前的光标。

最佳答案

因为您要求非标准行为,所以您需要自己进行“额外”提升,这需要您有一个 MouseListenermousePressed 设置光标并 mouseReleased 使其静止(因此,如果用户只是单击而不拖动组件,则不会出现愚蠢的光标状态)和 DragGestureListener# DragDropEnd 也会重置光标。

DragDrop

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DragGestureEvent;
import java.awt.dnd.DragGestureListener;
import java.awt.dnd.DragSource;
import java.awt.dnd.DragSourceDragEvent;
import java.awt.dnd.DragSourceDropEvent;
import java.awt.dnd.DragSourceEvent;
import java.awt.dnd.DragSourceListener;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;
import javax.swing.border.MatteBorder;

public class DragAndDropTest {

public static void main(String[] args) {
new DragAndDropTest();
}

public DragAndDropTest() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class TestPane extends JPanel {

public TestPane() {
setLayout(new GridLayout(1, 2));
add(new DropPane());
add(new DragPane());
}

@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}

}

public class DragPane extends JPanel {

private DragSource ds;
private Transferable transferable;

public DragPane() {
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
}

@Override
public void mouseReleased(MouseEvent e) {
setCursor(Cursor.getDefaultCursor());
}
});
ds = new DragSource();
transferable = new Transferable() {

@Override
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[]{DataFlavor.stringFlavor};
}

@Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
return DataFlavor.stringFlavor.equals(flavor);
}

@Override
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
return "This is a test";
}
};
ds.createDefaultDragGestureRecognizer(this, DnDConstants.ACTION_COPY_OR_MOVE, new DragGestureListener() {
@Override
public void dragGestureRecognized(DragGestureEvent dge) {
// This is where you would export the data you want
// to transfer
ds.startDrag(dge, Cursor.getPredefinedCursor(Cursor.HAND_CURSOR), transferable, new 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) {
setCursor(Cursor.getDefaultCursor());
}

});
}
});

setLayout(new GridBagLayout());
add(new JLabel("Drag from here"));
setBorder(new LineBorder(Color.RED));
}

}

public class DropPane extends JPanel {

private List<Point> dropPoints;

public DropPane() {
dropPoints = new ArrayList<>(25);
setDropTarget(new DropTarget(this, new DropTargetListener() {

@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) {
// Normally here, I'd inspect the Transferable and make sure
// what is been dropped and can be imported, I'd then go through
// the process of unwrapping the data from the Transferable and
// processing it appropriatly, but in this example, I really don't
// care, I just care about WHERE the event occured
dropPoints.add(dtde.getLocation());
dtde.dropComplete(true);
repaint();
}
}));
setLayout(new GridBagLayout());
add(new JLabel("Drop to here"));
setBorder(new MatteBorder(1, 1, 1, 0, Color.RED));
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
for (Point p : dropPoints) {
g.fillOval(p.x - 2, p.y - 2, 5, 5);
}
}

}

}

现在,记住,你正在做一些系统不希望你做的事情,所以它可能会以任何方式转过来告诉你

我可能会考虑编写一个工厂方法,它引用ComponentTransferable(可能还有Cursor)并构建并注册MouseListenerDragSource

关于java - 是否可以使用 java Swing DnD api 在 mousePressed 上开始拖动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36590276/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com