持续的SWT外壳窗口位置



我的代码类似于下面的示例所做的,该代码使我可以在其他位置打开外壳。我需要做的是跟踪窗口位置,如果它被移动并在下次打开窗口时保存这些位置。有什么建议吗?

 public StartupSplashShell(Display display)
 {
     shell = new Shell(display, SWT.NO_TRIM);
     setupShell();  // place components in the main avoCADo shell
     shell.setText("avoCADo");
     shell.setBackgroundImage(ImageUtils.getIcon("./avoCADo-Splash.jpg", 
     360, 298));
     shell.setSize(360, 298);   //TODO: set intial size to last known size
     Rectangle b = display.getBounds();
     int xPos = Math.max(0, (b.width-360)/2);
     int yPos = Math.max(0, (b.height-298)/2);
     shell.setLocation(xPos, yPos);
     shell.setImage(ImageUtils.getIcon("./avoCADo.png", 32, 32));
     shell.open();
}

在您的退出应用程序处理程序中注入IEclipsePreferences,如果您在Eclipse插件上工作,则将界限保存在Eclipse首选项中。

@Inject
@Preference
private IEclipsePreferences preferences;

如果您的应用程序是独立的SWT应用程序,则可以使用文件(例如适当的)或数据库来持续shell

的界限
mainShell.getBounds() // serialize it in String
preferences.put("SHELL_BOUNDS", boundStr);

当您的应用程序启动并从首选项中检索边界

时再次注入首选项
bounds = preferences.get("SHELL_BOUNDS", "");

然后您可以设置外壳的位置和大小

mainShell.setLocation(xAxis, yAxis);
mainShell.setSize(width, height);

假设这是一个普通的SWT应用程序,您可以使用SWT.Move侦听器每次移动时都会被告知:

shell.addListener(SWT.Move, event ->
  { 
    Point location = shell.getLocation();
    ....
  });

或者您可以使用SWT.Close侦听器来收听外壳关闭:

shell.addListener(SWT.Close, event ->
  { 
    Point location = shell.getLocation();
    ....
  });

如果要在应用程序的运行之间保存位置,则必须将位置保存在Properties文件之类的位置。

最新更新