描述:
在 mouseUp中实现移位,跟随鼠标移动正常。在 mouseMove中实现移位,跟随鼠标移动不正常。求教~
截图:
源码:
- Java code
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) { } } ); } }}