gpt4 book ai didi

java - 在 swing/Java 中为自定义 DnD 设置 dragImage

转载 作者:行者123 更新时间:2023-11-30 09:13:50 25 4
gpt4 key购买 nike

我正在使用 this example 来实现自定义 DnD。我想制作自己的 DragImage 并进行设置,但此示例不使用传输处理程序。那么我应该在哪里设置dragImage呢?

我正在为一个复合组件实现 DnD,其中包括一个具有更多组件的 JPanel,这个示例以及我的代码运行良好,我只需要在某处为其设置动态 DragImage。拖动图像不是固定的,它是在拖动开始后生成的,正如我在 another question [问题] 中所述。


注意:我添加此信息是为了解释我所做的更多尝试:

我疯狂的 transferHandler:

TransferHandler transferHandler = new TransferHandler(){
private static final long serialVersionUID = 5071745666227627589L;

@Override
public boolean canImport(TransferSupport support) {
boolean res = super.canImport(support);

return res;
}

@Override
public Icon getVisualRepresentation(Transferable t) {
// return super.getVisualRepresentation(t);
return new ImageIcon(getDragImage());
}

@Override
public Point getDragImageOffset() {
return new Point(0, 0);
}

@Override
public int getSourceActions(JComponent c) {
int res = super.getSourceActions(c);
setDragImage(getDragImage());
return res;
}

@Override
public boolean importData(TransferSupport support) {
boolean res = super.importData(support);
setDragImage(getDragImage());
return res;
}




@Override
public void setDragImage(Image img) {
super.setDragImage(img);
}

@Override
public Image getDragImage() {
BufferedImage image = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
printAll(g);
g.dispose();
//setDragImage(image);
return image;
}

};

setTransferHandler(transferHandler);

我试图解决在 dragGestureRecognized 中更改光标的问题,它将手势更改为组件的微型图像,但图像太小了。像光标一样小。将光标设置为 null 也不起作用。

public void dragGestureRecognized(DragGestureEvent dge) {
//Cursor cursor = DragSource.DefaultCopyDrop;
Cursor c;
c = Toolkit.getDefaultToolkit().createCustomCursor(jiJPanel.getTransferHandler().getDragImage(), new Point(0,0), "");
dge.startDrag(c, new TransferableQuadrate(qi));
}

};

最佳答案

也许这会有所帮助?我有这段旧代码,我只是添加了一行代码来调用 ComponentHandler 类中的 setDragImage() 方法。它使用 Screen Image类来创建您正在移动的组件的图像。我没有缩放图像或任何东西,所以你得到了整个组件。

import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.event.*;
import java.beans.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.plaf.*;
import javax.swing.text.*;
import java.io.*;

public class DragComponent extends JPanel
{
// public final static DataFlavor COMPONENT_FLAVOR = new DataFlavor(Component[].class, "Component Array");
public static DataFlavor COMPONENT_FLAVOR;

public DragComponent()
{
try
{
COMPONENT_FLAVOR = new DataFlavor(DataFlavor.javaJVMLocalObjectMimeType + ";class=\"" + Component[].class.getName() + "\"");
}
catch(Exception e)
{
System.out.println(e);
}

setLayout(null);
setTransferHandler( new PanelHandler() );

MouseListener listener = new MouseAdapter()
{
@Override
public void mousePressed(MouseEvent e)
{
JComponent c = (JComponent) e.getSource();
TransferHandler handler = c.getTransferHandler();
handler.exportAsDrag(c, e, TransferHandler.MOVE);
}
};

TransferHandler handler = new ComponentHandler();

for (int i = 0; i < 5; i++)
{
JLabel label = new JLabel("Label " + i);
label.setSize( label.getPreferredSize() );
label.setLocation(30 * (i+1), 30 * (i+1));
label.addMouseListener( listener );
label.setTransferHandler( handler );
add( label );
}

JPanel panel = new JPanel();
panel.add( new JLabel("one") );
panel.add( new JButton("two") );
panel.setLocation(0, 180);
panel.setSize( panel.getPreferredSize() );
panel.addMouseListener( listener );
panel.setTransferHandler( handler );
add(panel);

}

private static void createAndShowUI()
{
DragComponent north = new DragComponent();
north.setBackground(Color.RED);
north.setPreferredSize( new Dimension(200, 200) );

DragComponent south = new DragComponent();
south.setBackground(Color.YELLOW);
south.setPreferredSize( new Dimension(200, 200) );

JFrame frame = new JFrame("DragComponent");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(north, BorderLayout.NORTH);
frame.add(south, BorderLayout.SOUTH);
frame.pack();
frame.setLocationByPlatform( true );
frame.setVisible( true );
}

public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}

class ComponentHandler extends TransferHandler
{
@Override
public int getSourceActions(JComponent c)
{
setDragImage( ScreenImage.createImage(c) );

return MOVE;
}

@Override
public Transferable createTransferable(final JComponent c)
{
return new Transferable()
{
@Override
public Object getTransferData(DataFlavor flavor)
{
Component[] components = new Component[1];
components[0] = c;
return components;
}

@Override
public DataFlavor[] getTransferDataFlavors()
{
DataFlavor[] flavors = new DataFlavor[1];
flavors[0] = DragComponent.COMPONENT_FLAVOR;
return flavors;
}

@Override
public boolean isDataFlavorSupported(DataFlavor flavor)
{
return flavor.equals(DragComponent.COMPONENT_FLAVOR);
}
};
}

@Override
public void exportDone(JComponent c, Transferable t, int action)
{
System.out.println(c.getBounds());
}
}

class PanelHandler extends TransferHandler
{
@Override
public boolean canImport(TransferSupport support)
{
if (!support.isDrop())
{
return false;
}

boolean canImport = support.isDataFlavorSupported(DragComponent.COMPONENT_FLAVOR);
return canImport;
}

@Override
public boolean importData(TransferSupport support)
{
if (!canImport(support))
{
return false;
}

Component[] components;

try
{
components = (Component[])support.getTransferable().getTransferData(DragComponent.COMPONENT_FLAVOR);
}
catch (Exception e)
{
e.printStackTrace();
return false;
}

Component component = components[0];
System.out.println(component.getClass());
Container container = (Container)support.getComponent();
container.add(component);
// container.revalidate();
// container.repaint();
container.getParent().revalidate();
container.getParent().repaint();

// JLabel label = (JLabel)component;
DropLocation location = support.getDropLocation();
// System.out.println(label.getText() + " + " + location.getDropPoint());
component.setLocation( location.getDropPoint() );

return true;
}
}

关于java - 在 swing/Java 中为自定义 DnD 设置 dragImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20831411/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com