📄 dragdrop.java
字号:
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DragSource;
import org.eclipse.swt.dnd.DragSourceAdapter;
import org.eclipse.swt.dnd.DragSourceEvent;
import org.eclipse.swt.dnd.DropTarget;
import org.eclipse.swt.dnd.DropTargetAdapter;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class DragDrop {
protected Shell shell;
/**
* Launch the application
* @param args
*/
public static void main(String[] args) {
try {
DragDrop window = new DragDrop();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window
*/
public void open() {
final Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
/**
* Create contents of the window
*/
protected void createContents() {
shell = new Shell();
shell.setSize(500, 375);
shell.setText("SWT Application");
//
final StyledText text1 = new StyledText(shell,1);
text1.setBounds(10, 10, 262, 65);
text1.setText("aaaaaaaaaaaaaaaaaaaaaaaaaaa");
DragSource source = new DragSource(text1,DND.DROP_COPY|DND.DROP_MOVE);
source.setTransfer(new Transfer[] {TextTransfer.getInstance()});
source.addDragListener(new DragSourceAdapter() {
Point selection;
public void dragStart(DragSourceEvent e) {
selection = text1.getSelection();
e.doit = selection.x != selection.y;
}
public void dragSetData(DragSourceEvent e) {
e.data = text1.getText(selection.x, selection.y-1);
}
public void dragFinished(DragSourceEvent e) {
if (e.detail == DND.DROP_MOVE) {
text1.replaceTextRange(selection.x, selection.y - selection.x, " ");
}
selection = null;
}
});
final StyledText text2 = new StyledText(shell, 1);
text2.setBounds(40, 199, 232, 91);
text2.setText("bbbbbbbbbbbbbbb");
DropTarget target = new DropTarget(text2, DND.DROP_DEFAULT | DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_LINK);
target.setTransfer(new Transfer[] {TextTransfer.getInstance()});
target.addDropListener(new DropTargetAdapter() {
public void dragEnter(DropTargetEvent e) {
if (e.detail == DND.DROP_DEFAULT)
e.detail = DND.DROP_COPY;
}
public void dragOperationChanged(DropTargetEvent e) {
if (e.detail == DND.DROP_DEFAULT)
e.detail = DND.DROP_COPY;
}
public void drop(DropTargetEvent e) {
text2.insert((String)e.data);
}
});
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -