[英]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.