继承自的类org.eclipse.swt.widgets.Group继承Composite自Scrollable。但是,当我调用继承的方法getVerticalScrollBar()时,它总是返回null.
是否可以制作Group可滚动的?如果可能,怎么做?
顺便说一句,Group包含许多org.eclipse.swt.widgets.Button.Button样式为SWT.CHECK.
继承自的类org.eclipse.swt.widgets.Group继承Composite自Scrollable。但是,当我调用继承的方法getVerticalScrollBar()时,它总是返回null.
是否可以制作Group可滚动的?如果可能,怎么做?
顺便说一句,Group包含许多org.eclipse.swt.widgets.Button.Button样式为SWT.CHECK.
我不认为你可以得到Group滚动本身。但是,我设法通过添加 aScrolledComposite来达到相同的效果Group:
group = new Group(parent, SWT.NONE);
group.setText("Images: ");
group.setLayout(new FillLayout());
ScrolledComposite comp = new ScrolledComposite(group, SWT.V_SCROLL | SWT.H_SCROLL);
comp.setLayout(new FillLayout());
Composite innerComp = new Composite(comp, SWT.NONE);
innerComp.setLayout(new FillLayout());
comp.setContent(innerComp);
comp.setExpandHorizontal(true);
comp.setExpandVertical(true);
之后,您可以将您Button的 s 添加到Composite innerComp. 可能需要setMinHeight(int height)在setMinWidth(int width).ScrolledComposite
编辑
这是一个小的工作示例。如果您减小窗口的大小,您会看到滚动条会出现:
public static void main(String[] args)
{
Display display = Display.getDefault();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
Group imageGroup = new Group(shell, SWT.V_SCROLL | SWT.H_SCROLL);
imageGroup.setText("Images: ");
imageGroup.setLayout(new FillLayout());
ScrolledComposite frontComp = new ScrolledComposite(imageGroup, SWT.V_SCROLL | SWT.H_SCROLL);
Composite innerFrontComp = new Composite(frontComp, SWT.NONE);
frontComp.setLayout(new FillLayout());
innerFrontComp.setLayout(new FillLayout());
new Button(innerFrontComp, SWT.CHECK).setText("Button");
new Button(innerFrontComp, SWT.CHECK).setText("Button");
new Button(innerFrontComp, SWT.CHECK).setText("Button");
new Button(innerFrontComp, SWT.CHECK).setText("Button");
new Button(innerFrontComp, SWT.CHECK).setText("Button");
new Button(innerFrontComp, SWT.CHECK).setText("Button");
new Button(innerFrontComp, SWT.CHECK).setText("Button");
new Button(innerFrontComp, SWT.CHECK).setText("Button");
frontComp.setMinHeight(innerFrontComp.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
frontComp.setMinWidth(innerFrontComp.computeSize(SWT.DEFAULT, SWT.DEFAULT).x);
frontComp.setContent(innerFrontComp);
frontComp.setExpandHorizontal(true);
frontComp.setExpandVertical(true);
shell.pack();
shell.open();
while(!shell.isDisposed())
{
if(!display.readAndDispatch())
display.sleep();
}
}
SWT的Javadoc说:
如果有,则返回接收者的垂直滚动条,如果没有则返回 null。
如果用户使窗口/对话框变小,则出现滚动条。只有这样,这些方法才会getVerticalScrollBar()返回getHorizontalScrollBar()一个非空值。您必须始终检查null何时涉及此问题。这就是 SWT 的构建方式。
编辑:尝试在 a或 a上调用该getVerticalScrollBar()方法。CompositeShell