恒定宽度SashForm [英] Constant Width SashForm

查看:43
本文介绍了恒定宽度SashForm的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序有一个带有两个孩子的SashForm。我希望在调整窗口大小时,左子窗口的大小保持不变。我希望Eclipse对包资源管理器和主编辑器做同样的事情。调整窗口大小时,只有文本编辑器更改大小。但是,包资源管理器仍可按窗框调整大小。

我尝试使用以下

sashForm.addControlListener(new ControlAdapter() {
    @Override
    public void controlResized(ControlEvent e) {
        int width = sashForm.getClientArea().width;
        int[] weights = sashForm.getWeights();
        weights[1] = width - weights[0];
        sashForm.setWeights(weights);
    }
});

问题是左侧大小的宽度要么缩小到0,要么扩展太多。看起来权重在调用此函数或其他什么之前已更新。

如果我将权重[0]设置为等于某个常量,则它会执行我想要的操作。

推荐答案

这是我能想到的最佳解决方案。

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

public class Main {
private static int leftWidth, oldWeight;

public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("StackOverflow");
    shell.setLayout(new FillLayout());
    shell.setSize(600, 400);

    final SashForm form = new SashForm(shell, SWT.HORIZONTAL);

    final Button button = new Button(form, SWT.PUSH);
    button.setText("Left");

    button.addListener(SWT.Resize, new Listener() {
        @Override
        public void handleEvent(Event arg0) {
            int[] weights = form.getWeights();
            // oldWeights is used to distinguish between a window resize and
            // a sash move
            if (oldWeight != weights[0]) {
                System.out.println("Weights changed!");
                oldWeight = weights[0];
                leftWidth = (int) Math.round((double) form.getClientArea().width
                        * (double) weights[0]
                        / (double) (weights[0] + weights[1]));
            }
        }
    });

    Button buttonR = new Button(form, SWT.PUSH);
    buttonR.setText("Right");

    form.setWeights(new int[] { 200, 800 });
    leftWidth = 200;

    form.addListener(SWT.Resize, new Listener() {
        @Override
        public void handleEvent(Event arg0) {
            int width = form.getClientArea().width;
            int[] weights = form.getWeights();

            double perChange = (double) leftWidth / (double) width;

            weights[0] = (int) (perChange * 1000.0);
            weights[1] = 1000 - weights[0];

            // oldWeights must be set before form.setWeights
            oldWeight = weights[0];
            form.setWeights(weights);
        }
    });

    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

由于舍入问题而调整窗口大小时,窗框跳跃了大约1px,但它似乎做了我想要做的事情。

这是一个非常老套的解决方案,我想知道是否有更好的解决方案。如果您使窗口比左按钮小,它也会崩溃,但这很容易修复。

这篇关于恒定宽度SashForm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆