我正在努力用 JAVA SWT 创建 UI。
一件事让我对使用 ScrolledComposite 感到困惑。为什么我的代码在不设置复合的 MinSize 的情况下不显示滚动?
谁能解释 setMinSize 在 ScrolledComposite 中的作用?
为什么我们不能使用" setMinSize " 代替"setSize " ?
下面是我的代码。
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class MyPractice {
public static void main(String[] args) {
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(7, true));
c.setBackground(new Color(display, 0,255,0));
for (int i = 0; i < 200; i++) {
Button b = new Button(c, SWT.PUSH);
b.setText("Button " + i);
}
sc.setContent(c);
sc.setExpandHorizontal(true);
sc.setExpandVertical(true);
//*****Why is this code necessary to get Scroll on UI?
sc.setMinSize(c.computeSize(SWT.DEFAULT, SWT.DEFAULT));
//Why can't we use "setSize" insetead of setMinSize? setSize does not show scroll.
//sc.setSize(c.computeSize(SWT.DEFAULT, SWT.DEFAULT));
shell.setSize(300, 500);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}