繁体   English   中英

当鼠标在应用程序窗口之外时,是否可以在从摇摆中拖动(我的意思是图像,而不是位置)时更改鼠标光标?

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM