【问题标题】:Persistent SWT shell window positions持久 SWT shell 窗口位置
【发布时间】:2019-02-14 21:05:59
【问题描述】:

我的代码类似于下面的示例,它允许我在不同的位置打开一个 shell。我需要做的是跟踪窗口位置(如果它被移动)并保存这些位置以供下次打开该窗口时使用。有什么建议吗?

 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();
}

【问题讨论】:

  • 这是您遇到问题的代码吗?
  • 这只是一个普通的 SWT 应用程序还是 Eclipse RCP 的一部分或...?

标签: eclipse shell swt


【解决方案1】:

如果您正在使用 Eclipse 插件,请在退出应用程序处理程序中注入 IEclipsePreferences 并将边界保存在 Eclipse 首选项中。

@Inject
@Preference
private IEclipsePreferences preferences;

如果您的应用程序是独立的 SWT 应用程序,那么您可以使用文件(例如属性)或数据库来保持 shell 的边界

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

在您的应用程序启动时再次注入首选项并从首选项中检索边界

bounds = preferences.get("SHELL_BOUNDS", "");

然后你可以设置shell的位置和大小

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

【讨论】:

  • 这假定一个 Eclipse 插件(和注入的东西),问题标签只是说 SWT。
  • 是的,这是我理解的错误。为了存储目的,甚至可以使用属性。
【解决方案2】:

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

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

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

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

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

【讨论】:

  • 恢复时记得检查位置是否还在屏幕上。根据显示器设置,它可能不会。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多