when I drag the composite edge,It not work very well!Why? [message #1821974] |
Tue, 25 February 2020 01:00 |
zhang lei Messages: 22 Registered: July 2012 |
Junior Member |
|
|
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.*;
public class DragCompositeTest {
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display);
shell.open();
final Composite composite = new Composite(shell, SWT.BORDER);
composite.setBounds(0, 200, 200, 200);
//composite.setBackground(display.getSystemColor(SWT.COLOR_DARK_GRAY));
Label separator = new Label(composite, SWT.HORIZONTAL | SWT.SEPARATOR);
separator.setText("hello");
separator.setBounds(0, 0, composite.getBounds().width, 10);
//separator.addListener(SWT.MouseDown, listenerSeparator);
Listener listenerSeparator = new Listener(){
boolean booleanMouseDown = false;
Point pointMouseDown = null ;//鼠标按下去的点的位置
Point pointMouseMove = null;
@Override
public void handleEvent(Event arg0) {
// TODO Auto-generated method stub
switch (arg0.type) {
case SWT.MouseDown:
booleanMouseDown = true;
pointMouseDown =new Point(arg0.x,arg0.y);
break;
case SWT.MouseMove: // FALL THROUGH
Cursor cursor = new Cursor(display, SWT.CURSOR_SIZEN);
separator.setCursor(cursor);
if(booleanMouseDown){
//composite.setBounds(composite.getBounds().x, composite.getBounds().y-5, composite.getBounds().width-1, composite.getBounds().height+5);
pointMouseDown = separator.toDisplay(pointMouseDown);
pointMouseMove = separator.toDisplay(new Point(arg0.x,arg0.y));
//System.out.println("鼠标按下:"+pointMouseDown+" 移动鼠标:"+pointMouseMove);
composite.setBounds(composite.getBounds().x,composite.getBounds().y+(pointMouseMove.y-pointMouseDown.y),
composite.getBounds().width,composite.getBounds().height+(pointMouseDown.y-pointMouseMove.y));
//System.out.println("偏移量:"+(pointMouseDown.y-pointMouseMove.y)+"composite边界:"+composite.getBounds());
pointMouseDown = new Point(arg0.x,arg0.y);
}
cursor.dispose();
break;
case SWT.MouseUp:
booleanMouseDown = false;
break;
}
}
};
separator.addListener(SWT.MouseMove, listenerSeparator);
separator.addListener(SWT.MouseDown, listenerSeparator);
separator.addListener(SWT.MouseUp, listenerSeparator);
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
The reason? could you give a solution?Thank you very much!
[Updated on: Tue, 25 February 2020 02:41] Report message to a moderator
|
|
|
|
Powered by
FUDForum. Page generated in 0.03435 seconds