eclipse 将 abel 包裹在复合材料中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11772305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Wrap abel within a composite
提问by humansg
I have ScrolledComposite
which allows only vertical scrolling. (heighthint = 400
).
我有ScrolledComposite
它只允许垂直滚动。( heighthint = 400
).
Within this ScrolledComposite, I have another CompositeA
(height may exceed 400 for scrolling) to store all other widgets.
在这个 ScrolledComposite 中,我有另一个CompositeA
(滚动高度可能超过 400)来存储所有其他小部件。
I have a very long label (with SWT.WRAP
enabled). But instead of wrapping, it is always showing in a single line. I want this label to wrap according to the width of its parent (CompositeA
)
我有一个很长的标签(已SWT.WRAP
启用)。但它不是换行,而是始终显示在一行中。我希望这个标签根据其父级 ( CompositeA
)的宽度进行换行
I forgot to add that this CompositeA
is a 2 column GridLayout
with makeColumnsEqualWidth = true
.
我忘了补充一点,这CompositeA
是一个 2 列,GridLayout
带有makeColumnsEqualWidth = true
.
Here is my code:
这是我的代码:
public void createPartControl(Composite parent) {
// TODO Auto-generated method stub
Display display = parent.getDisplay();
toolkit = new FormToolkit(display);
form = toolkit.createForm(parent);
form.setText("ABC");
Composite body = form.getBody();
TableWrapLayout layout = new TableWrapLayout();
layout.numColumns = 2;
body.setLayout(layout);
Label header1 = toolkit.createLabel(body, "ABC: ");
Font font = new Font(display, "Arial", 11, SWT.BOLD);
header1.setFont(font);
Label header2 = toolkit.createLabel(body, "XYZ",
SWT.WRAP);
font = new Font(display, "Arial", 11, SWT.NONE);
header2.setFont(font);
TableWrapData wd = new TableWrapData(TableWrapData.FILL_GRAB);
header2.setLayoutData(wd);
form.getBody().setBackground(
form.getBody().getDisplay()
.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
// Scrolled composite
ScrolledComposite sc = new ScrolledComposite(body, SWT.BORDER_SOLID
| SWT.V_SCROLL);
sc.setAlwaysShowScrollBars(true);
sc.setBackground(new Color(display, 50,255,155));
wd = new TableWrapData(TableWrapData.FILL);
wd.heightHint = 360;
wd.colspan = 2;
wd.grabHorizontal = false;
sc.setLayoutData(wd);
sc.setLayout(new TableWrapLayout());
Composite innerComposite = toolkit.createComposite(sc);
sc.setContent(innerComposite);
innerComposite.setLayout(new TableWrapLayout());
innerComposite.setBackground(new Color(display, 255,50,50));
Section section = toolkit.createSection(innerComposite,
Section.DESCRIPTION | Section.TITLE_BAR | Section.EXPANDED);
wd = new TableWrapData(TableWrapData.FILL);
wd.maxWidth = 600; // don't want to hardcode this value
section.setLayoutData(wd);
section.setText("Section");
section.setDescription("A not so long description......................");
// Composite for Section
Composite sectionClient = toolkit.createComposite(section);
layout = new TableWrapLayout();
layout.numColumns = 2;
layout.makeColumnsEqualWidth = true;
sectionClient.setLayout(layout);
toolkit.createButton(sectionClient, "Button 1", SWT.RADIO);
Label rightDesc = toolkit
.createLabel(
sectionClient,
"A very long long long long long long long long long long long long long long long long long long long long desc that needs wrapping",
SWT.WRAP);
font = new Font(display, "Arial", 10, SWT.ITALIC);
rightDesc.setFont(font);
wd = new TableWrapData();
wd.rowspan = 2;
rightDesc.setLayoutData(wd);
Combo comboDropDown = new Combo(sectionClient, SWT.DROP_DOWN
| SWT.BORDER);
comboDropDown.setText("DDL");
comboDropDown.add("1");
comboDropDown.add("2");
comboDropDown.add("3");
Label lineBreak = toolkit.createSeparator(sectionClient, SWT.SEPARATOR
| SWT.HORIZONTAL);
wd = new TableWrapData(TableWrapData.FILL);
wd.colspan = 2;
lineBreak.setLayoutData(wd);
/***********************/
toolkit.createButton(sectionClient, "Button 2", SWT.RADIO);
Label rightDesc2 = toolkit
.createLabel(
sectionClient,
"A long long long long long long long long long long long long long long long long long long long long desc that needs wrapping",
SWT.WRAP);
font = new Font(display, "Arial", 10, SWT.ITALIC);
rightDesc2.setFont(font);
wd = new TableWrapData(TableWrapData.FILL);
wd.rowspan = 3;
rightDesc2.setLayoutData(wd);
toolkit.createLabel(sectionClient, "Desc",
SWT.WRAP);
toolkit.createText(sectionClient, "hello world", SWT.NONE);
section.setClient(sectionClient);
innerComposite.pack();
}
If you run it, you can see a green scrolledcomposite and a red composite. I want the red composite width to fill to the width of the scrolledcomposite relatively without hardcoding maxWidth = 600
.
如果你运行它,你可以看到一个绿色的滚动复合和一个红色的复合。我希望红色复合宽度在没有硬编码的情况下相对于 scrolledcomposite 的宽度填充maxWidth = 600
。
回答by ryanthara
I have the same problem here in my layout and found thisuseful answer.
我在我的布局中遇到了同样的问题,并找到了这个有用的答案。
Because of 'it is a not bug, it is a feature' try this answer from comment #19 here.
由于“它不是错误,它是一个功能”,请在此处尝试评论 #19 中的此答案。
I use the following lines of code for my Label.
我将以下代码行用于我的标签。
Label tip = new Label(shell, SWT.WRAP | SWT.BORDER | SWT.LEFT);
final GridData data = new GridData(SWT.HORIZONTAL, SWT.TOP, true, false, 1, 1);
tip.setLayoutData(data);
tip.setText("Here stands my long, long and much more longer text...");
回答by dac2009
Try adding SWT.WRAP when creating the Label. It worked for me.
在创建标签时尝试添加 SWT.WRAP。它对我有用。
Label label = new Label(parent, SWT.WRAP);
回答by dbalakirev
I might remember this wrong, but I recall I had to re-layout the composite when the text changed. That made the text wrap.
我可能记错了,但我记得当文本改变时我不得不重新布局复合材料。这使文本换行。
回答by Roman C
It was surprisingly to understand that TableWrapLayout
manager will not count on the composites that scrolls horizontally. To keep that layout on the Form
toolkit should be used. I've just made an example of code what could be done taking above.
令人惊讶的是,TableWrapLayout
经理不会指望水平滚动的复合材料。Form
应该使用工具包上的布局。我刚刚做了一个代码示例,上面可以做些什么。
public void createPartControl(Composite parent) {
// TODO Auto-generated method stub
Display display = parent.getDisplay();
toolkit = new FormToolkit(display);
form = toolkit.createForm(parent);
form.setText("ABC");
Composite body = form.getBody();
TableWrapLayout layout = new TableWrapLayout();
layout.numColumns = 2;
body.setLayout(layout);
Label header1 = toolkit.createLabel(body, "ABC: ");
Font font = new Font(display, "Arial", 11, SWT.BOLD);
header1.setFont(font);
Label header2 = toolkit.createLabel(body, "XYZ", SWT.WRAP);
font = new Font(display, "Arial", 11, SWT.NONE);
header2.setFont(font);
TableWrapData wd = new TableWrapData();
header2.setLayoutData(wd);
body.setBackground(body.getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
// Scrolled composite
Composite sc = toolkit.createComposite(body, SWT.BORDER_SOLID | SWT.V_SCROLL);
sc.setBackground(new Color(display, 50,255,155));
layout = new TableWrapLayout();
sc.setLayout(layout);
wd = new TableWrapData(TableWrapData.FILL_GRAB);
wd.heightHint = 360;
wd.colspan = 2;
sc.setLayoutData(wd);
Composite innerComposite = toolkit.createComposite(sc);
layout = new TableWrapLayout();
innerComposite.setLayout(layout);
innerComposite.setBackground(new Color(display, 255,50,50));
wd = new TableWrapData(TableWrapData.FILL_GRAB);
innerComposite.setLayoutData(wd);
Section section = toolkit.createSection(innerComposite,
Section.DESCRIPTION | Section.TITLE_BAR | Section.TWISTIE | Section.EXPANDED);
section.setText("Section");
section.setDescription("A not so long description......................");
wd = new TableWrapData(TableWrapData.FILL_GRAB);
// wd.maxWidth = 600; // don't want to hardcode this value
section.setLayoutData(wd);
// Composite for Section
Composite sectionClient = toolkit.createComposite(section);
layout = new TableWrapLayout();
layout.numColumns = 2;
// layout.makeColumnsEqualWidth = true;
sectionClient.setLayout(layout);
section.setClient(sectionClient);
wd = new TableWrapData(TableWrapData.FILL_GRAB);
sectionClient.setLayoutData(wd);
Label rightDesc = toolkit.createLabel(sectionClient,
"A very long long long long long long long long long long long long long long long long long long long long desc that needs wrapping",
SWT.WRAP);
font = new Font(display, "Arial", 10, SWT.ITALIC);
rightDesc.setFont(font);
wd = new TableWrapData();
wd.colspan = 2;
rightDesc.setLayoutData(wd);
Combo comboDropDown = new Combo(sectionClient, SWT.DROP_DOWN | SWT.BORDER);
comboDropDown.setText("DDL");
comboDropDown.add("1");
comboDropDown.add("2");
comboDropDown.add("3");
Label lineBreak = toolkit.createSeparator(sectionClient, SWT.SEPARATOR | SWT.HORIZONTAL);
wd = new TableWrapData(TableWrapData.FILL_GRAB);
lineBreak.setLayoutData(wd);
//
// /***********************/
//
Button button1 = toolkit.createButton(sectionClient, "Button 1", SWT.RADIO);
wd = new TableWrapData();
wd.colspan = 2;
button1.setLayoutData(wd);
Button button2 = toolkit.createButton(sectionClient, "Button 2", SWT.RADIO);
wd = new TableWrapData();
wd.colspan = 2;
button2.setLayoutData(wd);
Label rightDesc2 = toolkit.createLabel(sectionClient,
"A long long long long long long long long long long long long long long long long long long long long desc that needs wrapping",
SWT.WRAP);
font = new Font(display, "Arial", 10, SWT.ITALIC);
rightDesc2.setFont(font);
wd = new TableWrapData();
wd.colspan = 2;
rightDesc2.setLayoutData(wd);
Label desc = toolkit.createLabel(sectionClient, "Desc:");
Text hello = toolkit.createText(sectionClient, "hello world");
wd = new TableWrapData(TableWrapData.FILL_GRAB);
hello.setLayoutData(wd);
}