📄 mytransferhandler.java
字号:
import java.awt.Image;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
import javax.swing.JComponent;
import javax.swing.TransferHandler;
/*
* Created on 1/07/2006
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
/**
* @author Xuesong Le
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class MyTransferHandler extends TransferHandler
{
private static final long serialVersionUID = 1L;
MyPicComponent
sourceComp = null;
boolean
shouldRemove = false;
public boolean canImport(JComponent comp,DataFlavor[] transferFlavors)
{
for(int i=0; i< transferFlavors.length; i++)
{
if(transferFlavors[i].equals(DataFlavor.imageFlavor))
{
return true;
}
}// End for
return false;
}
public boolean importData(JComponent comp,Transferable t)
{
if( canImport(comp,t.getTransferDataFlavors()))
{
// Check data holding in Transferable t is same as that in comp
MyPicComponent
pic = (MyPicComponent)comp;
//Don't drop on myself.
if (sourceComp == pic)
{
shouldRemove = false;
return true;
}
else
{
try
{
// Get Transferable Data from t
Image
img = (Image)t.getTransferData(DataFlavor.imageFlavor);
// Import Data
pic.setImage(img);
return true;
}
catch (UnsupportedFlavorException ufe)
{
System.out.println("importData: unsupported data flavor");
}
catch (IOException ioe)
{
System.out.println("importData: I/O exception");
}
}
}
return false;
}
protected void exportDone(JComponent source,Transferable data,int action)
{
if( shouldRemove == true && action == MOVE)
{
MyPicComponent
sourceComp = (MyPicComponent)source;
sourceComp.setImage(null);
}
return;
}
protected Transferable createTransferable(JComponent c)
{
sourceComp = (MyPicComponent)c;
shouldRemove = true;
return new MyPicTransferable(sourceComp);
}
public int getSourceActions(JComponent c)
{
return COPY_OR_MOVE;
}
public static void main(String[] args)
{
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -