Constant Width SashForm(恒定宽度SashForm)
                            本文介绍了恒定宽度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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
				 沃梦达教程
				
			本文标题为:恒定宽度SashForm
				
        
 
            
        
             猜你喜欢
        
	     - 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
 - 获取数字的最后一位 2022-01-01
 - java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
 - 未找到/usr/local/lib 中的库 2022-01-01
 - 转换 ldap 日期 2022-01-01
 - Eclipse 的最佳 XML 编辑器 2022-01-01
 - GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
 - 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
 - 如何指定 CORS 的响应标头? 2022-01-01
 - 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
 
						
						
						
						
						
				
				
				
				