Java SWT 使标签可滚动
Java SWT make a Label Scrollable
我在 SWT 的 Group
中有一个 Label
,如果它包含多行文本,我想让它垂直滚动。使用 SWT.V_SCROLL
设置样式参数似乎并没有做到这一点。我该怎么做?
Label
不支持滚动。
您可以使用只读 Text
控件来滚动:
Text text = new Text(parent, SWT.READ_ONLY | SWT.V_SCROLL);
这里有一个方法
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();
我在 SWT 的 Group
中有一个 Label
,如果它包含多行文本,我想让它垂直滚动。使用 SWT.V_SCROLL
设置样式参数似乎并没有做到这一点。我该怎么做?
Label
不支持滚动。
您可以使用只读 Text
控件来滚动:
Text text = new Text(parent, SWT.READ_ONLY | SWT.V_SCROLL);
这里有一个方法
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();