Java shell.setLocation(x,y)相对于父区域而不是客户端区域
我有一个从父shell创建的自定义swt shell。 我需要设置外壳相对于其父组合的位置。但是,因为我在shell上调用setLocationx,y,所以setLocationx,y现在相对于clientArea工作。 是否有一种方法可以使shell.setLocationx,y相对于父组合而不是clientArea工作。即即使在屏幕上调整/移动父组合的大小,自定义外壳也应始终保持在其父组合中 示例代码段:Java shell.setLocation(x,y)相对于父区域而不是客户端区域,java,swt,Java,Swt,我有一个从父shell创建的自定义swt shell。 我需要设置外壳相对于其父组合的位置。但是,因为我在shell上调用setLocationx,y,所以setLocationx,y现在相对于clientArea工作。 是否有一种方法可以使shell.setLocationx,y相对于父组合而不是clientArea工作。即即使在屏幕上调整/移动父组合的大小,自定义外壳也应始终保持在其父组合中 示例代码段: class CustOmShellTest { customShell =
class CustOmShellTest {
customShell = new Shell(parent.getShell(), SWT.TOOL | SWT.CLOSE);
customShell.setLayout(new GridLayout());
customShell.setBackgroundMode(SWT.INHERIT_FORCE);
customShell.setSize(300, 400);
customShell.setLocation(parent.getBounds().x, parent.getBounds().y );
}
new CustOmShellTest(parentOfThisInstanceComposite);
//这是相对于显示定位的实例。我希望它是相对于此InstanceComposite的父级进行PSO处理的
感谢您的帮助!
谢谢。我创建了一个片段,其中自定义shell被锚定到主shell中的组件上。我称之为组件锚。用你的控件替换它 这里的神奇方法是Control.toDisplay,为了保持位置,必须添加resize和move侦听器
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new GridLayout());
final Label label = new Label(shell, SWT.NONE);
label.setText("anchor");
label.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true));
final Shell customShell = new Shell(shell, SWT.TOOL | SWT.CLOSE);
customShell.setLayout(new GridLayout());
customShell.setBackgroundMode(SWT.INHERIT_FORCE);
customShell.setSize(300, 400);
customShell.setVisible(true);
final Listener listener = new Listener() {
@Override
public void handleEvent(Event event) {
final Rectangle bounds = label.getBounds();
final Point absoluteLocation = label.toDisplay(bounds.width, bounds.height);
customShell.setLocation(absoluteLocation);
if (shell.getMaximized()) {
display.asyncExec(new Runnable() {
@Override
public void run() {
handleEvent(null);
}
});
}
}
};
shell.addListener(SWT.Resize, listener);
shell.addListener(SWT.Deiconify, listener);
shell.addListener(SWT.Move, listener);
customShell.addListener(SWT.Move, listener);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
请注意,我还在自定义shell上添加了侦听器,以确保它永远不会移动。当父外壳移动时,自定义外壳也随之移动。下面的代码获取实际外壳的相对位置 自定义shell的父级。这需要通过调整大小事件ie来完成 //parent.toDisplayparent.getLocation.x, //parent.getLocation.y
customShell.addListener(SWT.RESIZE, new Listener() {
public void handleEvent(final Event event) {
customShell.setLocation(parent.toDisplay(parent.getLocation().x ,
parent.getLocation().y));
customShell.layout();
parent.layout();
}
});
谢谢你的意见