0

我在放入ScrolledComposite里面时遇到问题Group- 它没有显示在Group. ScrolledComposite必须滚动Label

final private static Group group_netpbmOutput = new Group(shell, SWT.NONE);
final private static ScrolledComposite scroll_netpbmOutput = new ScrolledComposite(group_netpbmOutput, SWT.V_SCROLL | SWT.H_SCROLL);
final private static Label labelImage_netpbmOutput = new Label(scroll_netpbmOutput, SWT.BORDER);

//init Group
group_netpbmOutput.setLayout(new FillLayout());
formData = new FormData(((FormData)group_options.getLayoutData()).width, 200);
formData.top = new FormAttachment(group_options, 50, SWT.BOTTOM);
formData.left = new FormAttachment(group_options, 0, SWT.LEFT);
group_netpbmOutput.setLayoutData(formData);
group_netpbmOutput.setFont(font_group);
group_netpbmOutput.setText("Netpbm output");

//init Label
labelImage_netpbmOutput.setBackgroundImage(new Image(...));

//init ScrollComposite
scroll_netpbmOutput.setLayout(new FillLayout());
scroll_netpbmOutput.setContent(labelImage_netpbmOutput);

编辑:对巴兹回答的反应

那行得通,但是...图片以这种方式显示(应该只有一个'stackoverflow'):

final private static Group group_netpbmOutput = new Group(shell, SWT.NONE);
final private static ScrolledComposite scroll_netpbmOutput = new ScrolledComposite(group_netpbmOutput, SWT.V_SCROLL | SWT.H_SCROLL);
final private static Label labelImage_netpbmOutput = new Label(scroll_netpbmOutput, SWT.BORDER);

group_netpbmOutput.setLayout(new GridLayout());
formData = new FormData(((FormData)group_options.getLayoutData()).width, 200);
formData.top = new FormAttachment(group_options, 50, SWT.BOTTOM);
formData.left = new FormAttachment(group_options, 0, SWT.LEFT);
group_netpbmOutput.setLayoutData(formData);
group_netpbmOutput.setFont(font_group);
group_netpbmOutput.setText("Netpbm output");

labelImage_netpbmOutput.setBackgroundImage(new Image(...));

scroll_netpbmOutput.setLayout(new GridLayout(1, false));
scroll_netpbmOutput.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

scroll_netpbmOutput.setContent(labelImage_netpbmOutput);
scroll_netpbmOutput.setExpandHorizontal(true);
scroll_netpbmOutput.setExpandVertical(true);
scroll_netpbmOutput.setMinSize(labelImage_netpbmOutput.computeSize(SWT.DEFAULT, SWT.DEFAULT));
4

1 回答 1

4

不太清楚为什么您的代码不起作用。这可能是因为您没有设置ScrolledCompositevia的最小尺寸ScrolledComposite#setMinSize(Point)

不过,这绝对可以正常工作:

public static void main(String[] args)
{
    Display display = new Display();
    Shell shell = new Shell();
    shell.setText("StackOverflow");
    shell.setLayout(new GridLayout(1, false));

    Group group = new Group(shell, SWT.NONE);
    group.setText("Group");
    group.setLayout(new GridLayout(1, false));
    group.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    ScrolledComposite scroll = new ScrolledComposite(group, SWT.V_SCROLL | SWT.H_SCROLL);
    scroll.setLayout(new GridLayout(1, false));
    scroll.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    Image image = new Image(display, "stackoverflow.png");

    Label label = new Label(scroll, SWT.NONE);
    label.setImage(image);

    scroll.setContent(label);
    scroll.setExpandHorizontal(true);
    scroll.setExpandVertical(true);
    scroll.setMinSize(label.computeSize(SWT.DEFAULT, SWT.DEFAULT));

    shell.pack();
    shell.setSize(200, 200);
    shell.open();

    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
        {
            display.sleep();
        }
    }
    display.dispose();

    image.dispose();
}

看起来像这样:

在此处输入图像描述 在此处输入图像描述

于 2014-08-23T13:09:12.847 回答