【eclipse插件】编辑器中控件跟随鼠标移动的问题?
描述:
在 mouseUp中实现移位,跟随鼠标移动正常。在 mouseMove中实现移位,跟随鼠标移动不正常。求教~
截图:
源码:
package jsAppIde.editors;import org.eclipse.core.runtime.IProgressMonitor;import org.eclipse.swt.SWT;import org.eclipse.swt.events.MouseEvent;import org.eclipse.swt.events.MouseListener;import org.eclipse.swt.events.MouseMoveListener;import org.eclipse.swt.graphics.Color;import org.eclipse.swt.graphics.Cursor;import org.eclipse.swt.layout.GridData;import org.eclipse.swt.layout.GridLayout;import org.eclipse.swt.widgets.Composite;import org.eclipse.swt.widgets.Label;import org.eclipse.ui.IEditorInput;import org.eclipse.ui.IEditorSite;import org.eclipse.ui.PartInitException;import org.eclipse.ui.part.EditorPart;public class ModuleEditor extends EditorPart { public static final String ID = "jsAppIde.editors.ModuleEditor"; @Override public void doSave(IProgressMonitor monitor) { } @Override public void doSaveAs() { } @Override public void setFocus() { } @Override public void init(IEditorSite site, IEditorInput input) throws PartInitException { this.setSite(site); this.setInput(input); this.setPartName(input.getName()); } @Override public boolean isDirty() { return false; } @Override public boolean isSaveAsAllowed() { return false; } // 文本框"Fail":在 mouseMove中实现移位,跟随鼠标移动不正常 private Label failLbl = null; private boolean selectedFail = false; private int xFail = 0; private int yFail = 0; // 文本框"Ok":在 mouseUp中实现移位,跟随鼠标移动正常 private Label okLbl = null; private boolean selectedOk = false; private int xOk = 0; private int yOk = 0; @Override public void createPartControl(Composite parent) { parent.setLayout(new GridLayout(1, false)); // 创建面板 ModuleEditorPanel panel = new ModuleEditorPanel(parent, SWT.NONE); panel.setLayoutData(new GridData(600, 400)); panel.setLayout(null); // 设置面板背景色为黑色 panel.setBackground(new Color(parent.getDisplay(), 0, 0, 0)); // 创建文本框"Fail" if (this.failLbl == null) { this.failLbl = new Label(panel, SWT.NONE); this.failLbl.setBounds(200, 100, 80, 30); this.failLbl.setText("Fail"); this.failLbl.addMouseListener( new MouseListener() { @Override public void mouseUp(MouseEvent e) { if (selectedFail) { selectedFail = false; failLbl.setCursor(new Cursor(failLbl.getDisplay(), SWT.CURSOR_ARROW)); } } @Override public void mouseDown(MouseEvent e) { if (! selectedFail) { selectedFail = true; xFail = e.x; yFail = e.y; failLbl.setCursor(new Cursor(failLbl.getDisplay(), SWT.CURSOR_CROSS)); } } @Override public void mouseDoubleClick(MouseEvent e) { } } ); this.failLbl.addMouseMoveListener( new MouseMoveListener() { @Override public void mouseMove(MouseEvent e) { if (selectedFail) { int x2 = failLbl.getLocation().x + e.x - xFail; int y2 = failLbl.getLocation().y + e.y - yFail; failLbl.setLocation(x2, y2); xFail = e.x; yFail = e.y; } } } ); } // 创建文本框"Ok" if (this.okLbl == null) { this.okLbl = new Label(panel, SWT.NONE); this.okLbl.setBounds(200, 200, 80, 30); this.okLbl.setText("Ok"); this.okLbl.addMouseListener( new MouseListener() { @Override public void mouseUp(MouseEvent e) { if (selectedOk) { selectedOk = false; int x2 = okLbl.getLocation().x + e.x - xOk; int y2 = okLbl.getLocation().y + e.y - yOk; okLbl.setLocation(x2, y2); okLbl.setCursor(new Cursor(okLbl.getDisplay(), SWT.CURSOR_ARROW)); } } @Override public void mouseDown(MouseEvent e) { if (! selectedOk) { selectedOk = true; xOk = e.x; yOk = e.y; okLbl.setCursor(new Cursor(okLbl.getDisplay(), SWT.CURSOR_CROSS)); } } @Override public void mouseDoubleClick(MouseEvent e) { } } ); } }}
package testswt;import org.eclipse.swt.SWT;import org.eclipse.swt.events.MouseEvent;import org.eclipse.swt.events.MouseListener;import org.eclipse.swt.events.MouseMoveListener;import org.eclipse.swt.graphics.Cursor;import org.eclipse.swt.graphics.Point;import org.eclipse.swt.widgets.Display;import org.eclipse.swt.widgets.Label;import org.eclipse.swt.widgets.Shell;public class Snippet37 { // 文本框"Fail":在 mouseMove中实现移位,跟随鼠标移动不正常 private static Label failLbl = null; private static boolean selectedFail = false; // 文本框"Ok":在 mouseUp中实现移位,跟随鼠标移动正常 private static Label okLbl = null; private static boolean selectedOk = false; private static int xOk = 0; private static int yOk = 0; // 初始绝对坐标 private static int initRx = 0; private static int initRy = 0; // 控件初始坐标 private static int initX = 0; private static int inity = 0; public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(null); //shell.setLayout(new GridLayout()); // 创建文本框"Fail" if (failLbl == null) { failLbl = new Label(shell, SWT.BORDER); failLbl.setBounds(10, 10, 80, 30); failLbl.setText("Fail"); failLbl.addMouseListener( new MouseListener() { @Override public void mouseUp(MouseEvent e) { if (selectedFail) { selectedFail = false; failLbl.setCursor(new Cursor(failLbl.getDisplay(), SWT.CURSOR_ARROW)); } } @Override public void mouseDown(MouseEvent e) { if (! selectedFail) { selectedFail = true; Point location = failLbl.getLocation(); initX = location.x; inity = location.y; initRx = initX + e.x; initRy = inity + e.y; failLbl.setCursor(new Cursor(failLbl.getDisplay(), SWT.CURSOR_CROSS)); } } @Override public void mouseDoubleClick(MouseEvent e) { } } ); failLbl.addMouseMoveListener( new MouseMoveListener() { @Override public void mouseMove(MouseEvent e) { if (selectedFail) { int rx2 = failLbl.getLocation().x + e.x; int ry2 = failLbl.getLocation().y + e.y; int dx = rx2 - initRx; int dy = ry2 - initRy; failLbl.setLocation(initX + dx, inity + dy); System.out.println("event location2: (" + e.x + "," + e.y + ")"); } } } ); } // 创建文本框"Ok" if (okLbl == null) { okLbl = new Label(shell, SWT.BORDER); okLbl.setBounds(10, 50, 80, 30); okLbl.setText("Ok"); okLbl.addMouseListener( new MouseListener() { @Override public void mouseUp(MouseEvent e) { if (selectedOk) { selectedOk = false; int x2 = okLbl.getLocation().x + e.x - xOk; int y2 = okLbl.getLocation().y + e.y - yOk; okLbl.setLocation(x2, y2); okLbl.setCursor(new Cursor(okLbl.getDisplay(), SWT.CURSOR_ARROW)); } } @Override public void mouseDown(MouseEvent e) { if (! selectedOk) { selectedOk = true; xOk = e.x; yOk = e.y; okLbl.setCursor(new Cursor(okLbl.getDisplay(), SWT.CURSOR_CROSS)); } } @Override public void mouseDoubleClick(MouseEvent e) { } } ); } shell.setSize(200, 200); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); } }