[英]Is it possible to change mouse cursor when dragging (I mean image, not position) from swing when mouse is outside application window?
我multi-window java swing application
,在windows
支持之间drag&drop
。
我想全局更改mouse cursor
,即使它是在application windows
之间。
最明显的解决方案是Component.setCursor()
调用component
,它开始drag
或在主window
上不起作用。
然后,只有我发现这样做而不使用原生的,平台相关的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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.