我有multi-window java swing application和drag&drop之间的windows支持。
我想在全球范围内改变mouse cursor ,即使是在application windows之间。
最明显的解决方案是Component.setCursor(),在启动drag的component上调用它,或者在主window上调用它,它不起作用。
发布于 2016-04-13 06:52:11
然后,我发现不使用本机、依赖于平台的api的唯一方法是使用java Swing的DnD api,它允许您在拖动时设置自定义鼠标光标。
import javax.swing.*;
import java.awt.Cursor;
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(new Cursor(Cursor.HAND_CURSOR), new StringSelection(label.getText()));
};
DragSource dragSource = DragSource.getDefaultDragSource();
dragSource.createDefaultDragGestureRecognizer(label, DnDConstants.ACTION_COPY, dragGestureListener);
return label;
}
}https://stackoverflow.com/questions/36446972
复制相似问题