gpt4 book ai didi

java - 组内的 SWT 列表无法滚动

转载 作者:行者123 更新时间:2023-12-01 11:34:53 25 4
gpt4 key购买 nike

我需要一些有关 SWT 的帮助。我想在组中创建滚动列表。我使用以下代码,但组的大小是可变的,并且滚动条不可用。我想要一个具有给定大小的列表,如果列表中的条目太多,我想要一个滚动条来向下滚动。

    super(parent, style);

this.setLayout(new GridLayout(1,false));
Group grpSettings = new Group(this, SWT.NONE);
grpSettings.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
grpSettings.setText("Settings");
grpSettings.setLayout(new GridLayout(1, false));
grpSettings.setSize(200, 200);

Label nameLabel = new Label(grpSettings, SWT.NONE);
nameLabel.setText("Choose");

ScrolledComposite scroll = new ScrolledComposite(grpSettings, SWT.V_SCROLL | SWT.H_SCROLL);
scroll.setSize(200, 200);
scroll.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
scroll.setAlwaysShowScrollBars(true);

final List list = new List(scroll, SWT.NONE); // Create a List with a vertical ScrollBar
// Add a bunch of items to it

for (int i=1;i < 50 ;i++ ){
list.add("Ex" + i);
}

scroll.setContent(list);
scroll.setExpandHorizontal(true);
scroll.setExpandVertical(true);


list.addListener(SWT.Selection, new Listener () {
public void handleEvent (Event e) {
int es = list.getSelectionIndex();
System.out.println(es);
}
});
}

最佳答案

List 小部件有自己的滚动条,因此无需将其包装到 ScrolledComposite 中。

如下面的代码片段所示,使用 V_SCROLL 样式标志创建 List 就足够了

public static void main( String[] args ) {
Display display = new Display();
Shell shell = new Shell( display );
shell.setLayout( new FillLayout() );
Group group = new Group( shell, SWT.NONE );
group.setText( "Group" );
group.setLayout( new GridLayout( 1, false ) );
Label label = new Label( group, SWT.NONE );
label.setText( "Choose" );
List list = new List( group, SWT.V_SCROLL );
list.setLayoutData( new GridData( SWT.FILL, SWT.FILL, true, true ) );
for( int i = 0; i < 128; i++ ) {
list.add( "Item " + i );
}
shell.setSize( 300, 300 );
shell.open();
while( !shell.isDisposed() ) {
if( !display.readAndDispatch() )
display.sleep();
}
display.dispose();
}

关于java - 组内的 SWT 列表无法滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30105383/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com