我创建了一个 Shell 并向其中添加了一个 ScrolledComposite,其中包含一个 Text 作为其内容。但我希望外壳根据内容大小动态更改大小。我的实现如下
shell.setLayout(new GridLayout(1,true));
ScrolledComposite sc = new ScrolledComposite(shell, SWT.V_SCROLL|SWT.H_SCROLL);
sc.setLayoutData(GridDataFactory.fillDefaults().grab(true, true).hint(200, 200).create());
sc.setExpandHorizontal(true);
sc.setExpandVertical(true);
Composite top = new Composite(sc,SWT.NONE);
top.setLayout(GridLayoutFactory.swtDefaults().numColumns(1).create());
StyledText styledText = new StyledText(top, SWT.NONE);
styledText.setText(text);
StyleRange style = new StyleRange();
style.start = 0;
style.length = text.indexOf(":"); //$NON-NLS-1$
style.fontStyle = SWT.BOLD;
styledText.setStyleRange(style);
sc.setContent(top);
// shell.setSize(sc.computeSize(SWT.DEFAULT, SWT.DEFAULT));
sc.setMinSize(top.computeSize(SWT.DEFAULT, SWT.DEFAULT));
sc.pack(true);
shell.setVisible(true);
当我取消注释上面代码中的注释行时,shell 正在根据内容调整大小,但在这种情况下无法实现滚动条。
如果内容超出特定限制,我也想获得滚动条。如果内容在限制范围内,我不希望外壳有额外的空格。
有人可以在这里帮助我吗?