gpt4 book ai didi

Java SWT - 组内的 ScrolledComposite

转载 作者:行者123 更新时间:2023-12-01 18:27:44 29 4
gpt4 key购买 nike

我在将 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);

编辑:对巴兹回答的 react

这可行,但是......图片是这样显示的(应该只有一个“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));

最佳答案

不太确定为什么您的代码不起作用。这可能是因为您没有通过 ScrolledComposite#setMinSize(Point) 设置 ScrolledComposite 的最小大小。

这确实工作得很好:

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();
}

看起来像这样:

enter image description here enter image description here

关于Java SWT - 组内的 ScrolledComposite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25461240/

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