Java swt将对象拖动到;“死空间”;创建新窗口的步骤
我希望用我的软件模拟窗口拖动。我现在有画布,就像它自己的窗口一样。我在它们之间创建了一个拖放操作,这一切都非常可靠。然而,当我拖放到死空间时,我希望做一个拖动案例。或者更好的措辞是我的应用程序没有涵盖的领域。我的第一个想法是听鼠标移动到当前外壳之外,然后做一些事情。然而,我似乎无法找到一种方法来检测我是否在外壳之外 任何帮助都将不胜感激 我似乎找不到一种方法来检测我是否在壳外 如果您使用的是,您可以实现该方法并通过对象获得鼠标释放的点 请注意,此位置将相对于添加此侦听器的对象的父Shell。您需要将父Shell的位置添加到上面的(x,y)对中,以获得屏幕上的绝对位置 现在,您已经获得了单击某个目标对象后释放鼠标的点,您可以使用该方法检查该点是否在原始窗口之外 从这里,您可以执行创建新shell并将目标移动到该shell所需的任何工作Java swt将对象拖动到;“死空间”;创建新窗口的步骤,java,drag-and-drop,swt,Java,Drag And Drop,Swt,我希望用我的软件模拟窗口拖动。我现在有画布,就像它自己的窗口一样。我在它们之间创建了一个拖放操作,这一切都非常可靠。然而,当我拖放到死空间时,我希望做一个拖动案例。或者更好的措辞是我的应用程序没有涵盖的领域。我的第一个想法是听鼠标移动到当前外壳之外,然后做一些事情。然而,我似乎无法找到一种方法来检测我是否在外壳之外 任何帮助都将不胜感激 我似乎找不到一种方法来检测我是否在壳外 如果您使用的是,您可以实现该方法并通过对象获得鼠标释放的点 请注意,此位置将相对于添加此侦听器的对象的父Shell。您需
下面是我提供的一个工作示例,您可以将一个黄色矩形拖放到一个窗口外,它将创建一个新窗口,其中包含矩形:
public static void main(final String... args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("Original shell");
shell.setLayout(new FillLayout());
shell.setMaximized(true);
final Composite parentComposite = new Composite(shell, SWT.NONE);
parentComposite.setLayout(new GridLayout());
parentComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final Composite target = new Composite(parentComposite, SWT.NONE);
target.setBounds(new Rectangle(0, 0, 100, 100));
target.setBackground(new Color(display, 255, 255, 0));
target.addMouseListener(new MouseAdapter() {
@Override
public void mouseUp(final MouseEvent event) {
final Point releasePoint = new Point(event.x + target.getShell().getLocation().x,
event.y + target.getShell().getLocation().y);
if (!target.getShell().getBounds().contains(releasePoint)) {
// Create the new shell and base composite
final Shell newShell = new Shell(display);
newShell.setText("New shell");
newShell.setLayout(new FillLayout());
newShell.setMaximized(true);
final Composite newComposite = new Composite(newShell, SWT.NONE);
newComposite.setLayout(new GridLayout());
newComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
// Set the parent of the target widget to the new base composite
target.setParent(newComposite);
target.layout();
// Open the new shell
newShell.setLocation(releasePoint);
newShell.setSize(300, 300);
newShell.open();
}
}
});
shell.setSize(300, 300);
shell.open();
display.readAndDispatch();
while (!shell.isDisposed()) {
if (!shell.getDisplay().readAndDispatch()) {
shell.getDisplay().sleep();
}
}
}
编辑:只是澄清一下,如果您使用画布(您在问题中提到的画布)而不是复合对象作为
目标
对象,它的行为将完全相同
final Composite target = new Composite(parentComposite, SWT.NONE);
target.addMouseListener(new MouseAdapter() {
@Override
public void mouseUp(final MouseEvent event) {
final Point releasePoint = new Point(event.x + target.getShell().getLocation().x,
event.y + target.getShell().getLocation().y);
if (!target.getShell().contains(releasePoint)) {
// Dragged and dropped outside the window!
}
}
});
public static void main(final String... args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("Original shell");
shell.setLayout(new FillLayout());
shell.setMaximized(true);
final Composite parentComposite = new Composite(shell, SWT.NONE);
parentComposite.setLayout(new GridLayout());
parentComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final Composite target = new Composite(parentComposite, SWT.NONE);
target.setBounds(new Rectangle(0, 0, 100, 100));
target.setBackground(new Color(display, 255, 255, 0));
target.addMouseListener(new MouseAdapter() {
@Override
public void mouseUp(final MouseEvent event) {
final Point releasePoint = new Point(event.x + target.getShell().getLocation().x,
event.y + target.getShell().getLocation().y);
if (!target.getShell().getBounds().contains(releasePoint)) {
// Create the new shell and base composite
final Shell newShell = new Shell(display);
newShell.setText("New shell");
newShell.setLayout(new FillLayout());
newShell.setMaximized(true);
final Composite newComposite = new Composite(newShell, SWT.NONE);
newComposite.setLayout(new GridLayout());
newComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
// Set the parent of the target widget to the new base composite
target.setParent(newComposite);
target.layout();
// Open the new shell
newShell.setLocation(releasePoint);
newShell.setSize(300, 300);
newShell.open();
}
}
});
shell.setSize(300, 300);
shell.open();
display.readAndDispatch();
while (!shell.isDisposed()) {
if (!shell.getDisplay().readAndDispatch()) {
shell.getDisplay().sleep();
}
}
}