You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何设置Eclipse RCP应用默认初始启动窗口大小并保留用户自定义设置?

Eclipse 4.31 RCP 设置首次启动窗口默认尺寸并保留用户自定义设置

核心逻辑

  • 首次启动时强制应用1280×720(或其他16:9比例,如1920×1080)的窗口尺寸
  • 检测到用户已有窗口尺寸配置时,自动加载用户自定义设置,不再使用默认值

具体实现方法

方法一:E4模型处理器(纯E4应用推荐)

通过注册窗口模型处理器,在窗口初始化阶段读取偏好设置并调整尺寸,同时监听窗口关闭事件保存用户调整后的尺寸。

  1. 创建窗口初始化处理器类:
import org.eclipse.e4.core.di.annotations.PostConstruct;
import org.eclipse.e4.ui.model.application.ui.basic.MWindow;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.equinox.security.storage.ISecurePreferences;
import org.eclipse.equinox.security.storage.SecurePreferencesFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class WindowSizeInitializer {
    private static final String PREF_NODE_ID = "com.your.rcp.app.id";
    private static final String KEY_WIDTH = "window.width";
    private static final String KEY_HEIGHT = "window.height";
    private static final int DEFAULT_WIDTH = 1280;
    private static final int DEFAULT_HEIGHT = 720;

    @PostConstruct
    public void initWindow(MWindow window) {
        Shell shell = window.getWidget();
        if (shell == null || shell.isDisposed()) return;

        // 读取存储的尺寸偏好
        ISecurePreferences prefs = SecurePreferencesFactory.getDefault().node(PREF_NODE_ID);
        int width = prefs.getInt(KEY_WIDTH, DEFAULT_WIDTH);
        int height = prefs.getInt(KEY_HEIGHT, DEFAULT_HEIGHT);

        // 窗口居中显示
        Rectangle monitorBounds = Display.getDefault().getPrimaryMonitor().getBounds();
        int x = (monitorBounds.width - width) / 2;
        int y = (monitorBounds.height - height) / 2;
        shell.setBounds(x, y, width, height);

        // 监听窗口关闭事件,保存用户调整后的尺寸
        shell.addListener(SWT.Close, event -> {
            Rectangle currentBounds = shell.getBounds();
            prefs.putInt(KEY_WIDTH, currentBounds.width, true);
            prefs.putInt(KEY_HEIGHT, currentBounds.height, true);
        });
    }
}
  1. 在plugin.xml中注册处理器:
<extension
     point="org.eclipse.e4.core.di.extensions.modelProcessor">
   <processor
         class="com.your.rcp.app.WindowSizeInitializer"
         type="org.eclipse.e4.ui.model.application.ui.basic.MWindow">
   </processor>
</extension>

方法二:传统RCP Application类适配

如果你的应用基于传统IApplication实现,可在启动流程中直接处理窗口尺寸:

import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.application.WorkbenchAdvisor;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.equinox.security.storage.ISecurePreferences;
import org.eclipse.equinox.security.storage.SecurePreferencesFactory;

public class RcpApplication implements IApplication {
    private static final String PREF_NODE_ID = "com.your.rcp.app.id";
    private static final String KEY_WIDTH = "window.width";
    private static final String KEY_HEIGHT = "window.height";
    private static final int DEFAULT_WIDTH = 1280;
    private static final int DEFAULT_HEIGHT = 720;

    @Override
    public Object start(IApplicationContext context) throws Exception {
        Display display = PlatformUI.createDisplay();
        try {
            int returnCode = PlatformUI.createAndRunWorkbench(display, new WorkbenchAdvisor() {
                @Override
                public void postStartup() {
                    Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
                    if (shell == null) return;

                    // 读取偏好设置
                    ISecurePreferences prefs = SecurePreferencesFactory.getDefault().node(PREF_NODE_ID);
                    int width = prefs.getInt(KEY_WIDTH, DEFAULT_WIDTH);
                    int height = prefs.getInt(KEY_HEIGHT, DEFAULT_HEIGHT);

                    // 居中窗口
                    Rectangle monitor = display.getPrimaryMonitor().getBounds();
                    int x = (monitor.width - width) / 2;
                    int y = (monitor.height - height) / 2;
                    shell.setBounds(x, y, width, height);

                    // 保存用户调整后的尺寸
                    shell.addListener(SWT.Close, e -> {
                        Rectangle bounds = shell.getBounds();
                        prefs.putInt(KEY_WIDTH, bounds.width, true);
                        prefs.putInt(KEY_HEIGHT, bounds.height, true);
                    });
                }
            });
            return returnCode == PlatformUI.RETURN_RESTART ? EXIT_RESTART : EXIT_OK;
        } finally {
            display.dispose();
        }
    }

    @Override
    public void stop() {
        // 按需实现停止逻辑
    }
}

注意事项

  • 替换代码中的com.your.rcp.app.id为你的RCP应用插件ID
  • 首次启动时无存储偏好,自动使用1280×720默认尺寸;用户调整窗口并关闭后,新尺寸会被持久化,下次启动自动加载
  • 确保插件依赖org.eclipse.core.runtime、org.eclipse.equinox.security等必要组件

内容的提问来源于stack exchange,提问作者user1292548

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 14:46:16