我有一个swing应用程序,我想通过将外部文件从Windows资源管理器拖到应用程序来导入外部文件.我有这个基本功能.但是,我想将默认的拖放光标图标更改为适用于应用程序的光标.在按下鼠标键并将其保持在应用程序上时,我无法更改用户可见的光标.如果拖放操作在同一个swing应用程序中,我已经看到了这个工作的示例.我试图使用DragGestureListener和DragSource来实现这一点无济于事.似乎除非拖动源在摆动范围内,否则不会调用这些方法.将外部文件拖动到swing应用程序时是否可以更改拖动光标?
请看这个简化的例子:
public class DnDTemplate extends JFrame {
private static final long serialVersionUID = 1L;
private JComponent thePane = null;
private Cursor dropCursor = null;
public DnDTemplate() {
super( "Drop File Here" );
thePane = (JComponent) getContentPane();
thePane.setTransferHandler( new DndTransferHandler() );
ImageIcon imageIcon = new ImageIcon( "drop_here.gif" );
Image image = imageIcon.getImage();
BufferedImage bufferedImage = new BufferedImage( 16, 16, BufferedImage.TYPE_INT_ARGB );
Graphics graphics = bufferedImage.getGraphics();
graphics.drawImage( image, 0, 0, null );
dropCursor = Toolkit.getDefaultToolkit().createCustomCursor( bufferedImage, new Point( 16, 16 ), …Run Code Online (Sandbox Code Playgroud)