Java SWT使Label Scrollable成为可能

时间:2015-04-14 18:50:40

标签: java swt label

我在SWT的Label中有Group,如果它包含多行文字,我想让它垂直滚动。使用SWT.V_SCROLL设置样式参数似乎不会这样做。我怎么能这样做?

2 个答案:

答案 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();