如何在 gridlayout 中使用 scroll composite 启用滚动?
How enable scroll using scroll composite in gridlayout?
如果我使用 shell,下面的代码工作正常。但我使用的是复合材料,它不起作用。
如果我们使用 shell:
,代码工作正常
Display display = new Display ();
Shell shell = new Shell (display);
shell.setLayout(new GridLayout());
final ScrolledComposite sc = new ScrolledComposite(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
sc.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
Composite c = new Composite(sc, SWT.NONE);
c.setLayout(new GridLayout(10, true));
for (int i = 0 ; i < 300; i++) {
Button b = new Button(c, SWT.PUSH);
b.setText("Button "+i);
}
sc.setContent(c);
sc.setExpandHorizontal(true);
sc.setExpandVertical(true);
sc.setMinSize(c.computeSize(SWT.DEFAULT, SWT.DEFAULT));
sc.setShowFocusedControl(true);
shell.setSize(300, 500);
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
如果我们在 gridlayout 中使用组合,则相同的代码将无法运行:
Display display = new Display ();
Shell shell = new Shell (display);
shell.setLayout(new GridLayout());
Composite c1 = new Composite(shell, SWT.NONE);
c1.setLayout(new GridLayout());
final ScrolledComposite sc = new ScrolledComposite(c1, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
sc.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
Composite c = new Composite(sc, SWT.NONE);
c.setLayout(new GridLayout(10, true));
for (int i = 0 ; i < 300; i++) {
Button b = new Button(c, SWT.PUSH);
b.setText("Button "+i);
}
sc.setContent(c);
sc.setExpandHorizontal(true);
sc.setExpandVertical(true);
sc.setMinSize(c.computeSize(SWT.DEFAULT, SWT.DEFAULT));
sc.setShowFocusedControl(true);
shell.setSize(300, 500);
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
您并没有告诉您的 c1
复合材料相对于其父级应该如何表现(即设置布局数据)。
您有两个选择:
c1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
或
shell.setLayout(new FillLayout());
如果我使用 shell,下面的代码工作正常。但我使用的是复合材料,它不起作用。
如果我们使用 shell:
,代码工作正常 Display display = new Display ();
Shell shell = new Shell (display);
shell.setLayout(new GridLayout());
final ScrolledComposite sc = new ScrolledComposite(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
sc.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
Composite c = new Composite(sc, SWT.NONE);
c.setLayout(new GridLayout(10, true));
for (int i = 0 ; i < 300; i++) {
Button b = new Button(c, SWT.PUSH);
b.setText("Button "+i);
}
sc.setContent(c);
sc.setExpandHorizontal(true);
sc.setExpandVertical(true);
sc.setMinSize(c.computeSize(SWT.DEFAULT, SWT.DEFAULT));
sc.setShowFocusedControl(true);
shell.setSize(300, 500);
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
如果我们在 gridlayout 中使用组合,则相同的代码将无法运行:
Display display = new Display ();
Shell shell = new Shell (display);
shell.setLayout(new GridLayout());
Composite c1 = new Composite(shell, SWT.NONE);
c1.setLayout(new GridLayout());
final ScrolledComposite sc = new ScrolledComposite(c1, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
sc.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
Composite c = new Composite(sc, SWT.NONE);
c.setLayout(new GridLayout(10, true));
for (int i = 0 ; i < 300; i++) {
Button b = new Button(c, SWT.PUSH);
b.setText("Button "+i);
}
sc.setContent(c);
sc.setExpandHorizontal(true);
sc.setExpandVertical(true);
sc.setMinSize(c.computeSize(SWT.DEFAULT, SWT.DEFAULT));
sc.setShowFocusedControl(true);
shell.setSize(300, 500);
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
您并没有告诉您的 c1
复合材料相对于其父级应该如何表现(即设置布局数据)。
您有两个选择:
c1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
或
shell.setLayout(new FillLayout());