我在SWT的Label
中有Group
,如果它包含多行文字,我想让它垂直滚动。使用SWT.V_SCROLL
设置样式参数似乎不会这样做。我怎么能这样做?
答案 0 :(得分:7)
Label
不支持滚动。
您可以使用只读Text
控件滚动:
Text text = new Text(parent, SWT.READ_ONLY | SWT.V_SCROLL);
答案 1 :(得分:0)
这是一种方法
ScrolledComposite ivScrollComposite = new ScrolledComposite( ivShell, SWT.V_SCROLL );
ivScrollComposite.addControlListener( new ScrollCompositeControlListener() );
ivScrollComposite.setExpandVertical( true );
ivScrollComposite.setExpandHorizontal( true );
ivScrollComposite.setAlwaysShowScrollBars( true );
Composite ivCompositeResults = new Composite( ivScrollComposite, SWT.NONE );
Label ivLabelResults = new Label( ivCompositeResults, SWT.WRAP );
ivScrollComposite.setContent( ivCompositeResults );
ivScrollComposite.setLayout( new FormLayout() );
FormData formData = new FormData();
formData.top = new FormAttachment( 0, 0 );
formData.left = new FormAttachment( 0, 0 );
formData.right = new FormAttachment( 100, 0 );
formData.bottom = new FormAttachment( 100, 0 );
ivCompositeResults.setLayoutData( formData );
ivCompositeResults.setLayout( new FormLayout() );
formData = new FormData();
formData.top = new FormAttachment( 0, 0 );
formData.left = new FormAttachment( 0, 0 );
formData.right = new FormAttachment( 100, 0 );
formData.bottom = new FormAttachment( 100, 0 );
ivLabelResults.setLayoutData( formData );
然后
private void resizeScroll()
{
Rectangle r = ivScrollComposite.getClientArea();
ivScrollComposite.setMinSize( ivCompositeResults.computeSize( r.width, SWT.DEFAULT ) );
}
private class ScrollCompositeControlListener extends ControlAdapter
{
@Override
public void controlResized( ControlEvent e )
{
resizeScroll();
}
}
每当您更改标签文本时
ivLabelResults.setText( "some text" );
resizeScroll();