我创建了几个链接按钮。
我希望所有按钮都在同一行。
每次插入新按钮,它都会到达下一行。
public void createControls(Composite parent) {
myComposite = new Composite(parent, SWT.NONE);
GridLayout detailsSideGridLayout = new GridLayout();
detailsSideGridLayout.numColumns = 1;
myComposite.setLayout(detailsSideGridLayout);
GridDatamyGridData = new GridData();
myGridData.horizontalAlignment = SWT.FILL;
myGridData.grabExcessHorizontalSpace = true;
myGridData.verticalAlignment = SWT.FILL;
myGridData.grabExcessVerticalSpace = true;
myComposite.setLayoutData(myGridData);
deComposite = new Composite(myComposite, SWT.NONE);
RowLayout dgGridLayout = new RowLayout(SWT.HORIZONTAL);
deComposite.setLayout(deGridLayout);
GridData deGridData = new GridData();
deGridData.horizontalAlignment = SWT.FILL;
deGridData.grabExcessHorizontalSpace = true;
LinkLabel createDetailsde = createDetailsde(deComposite);
createDetailsde.setLayoutData(deGridData);
//Add another button
createLinkLabel(createDetailsde)
myComposite.layout();
private LinkLabel createDetailsde(Composite detailsComposite) {
// TODO check resource manager implementation
LinkLabel linkLabel = new LinkLabel(detailsComposite, SWT.NONE);
linkLabel.setText("test"); //$NON-NLS-1$
return linkLabel;
}
private LinkLabel createLinkLabel(Composite detailsComposite) {
LinkLabel linkLabel = new LinkLabel(detailsComposite, SWT.NONE);
linkLabel.setText("test2"); //$NON-NLS-1$
return linkLabel;
}
我收到了错误消息 错误是org.eclipse.swt.layout.GridData无法强制转换为org.eclipse.swt.layout.RowData
答案 0 :(得分:4)
您使用的GridLayout
只有一列(deGridLayout.numColumns = 1;
)。你可以做两件事:
GridLayout
定义为包含Button
s RowLayout
代替SWT.HORIZONTAL
(您无需在此处定义商品数量)以下是一些示例代码:
public static void main(String[] args)
{
Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("StackOverflow");
shell.setLayout(new GridLayout(1, false));
Group first = new Group(shell, SWT.NONE);
first.setText("RowLayout");
first.setLayout(new RowLayout(SWT.HORIZONTAL));
Group second = new Group(shell, SWT.NONE);
second.setText("GridLayout");
second.setLayout(new GridLayout(5, false));
for (int i = 1; i < 5; i++)
{
new Button(first, SWT.PUSH).setText("Button " + i);
new Button(second, SWT.PUSH).setText("Button " + i);
}
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
这就是它的样子:
您的代码存在的问题是,您尝试使用GridData
将Composite
添加到RowLayout
。那是不可能的。您的代码可以修复(并大规模压缩),如下所示:
public static void main(String[] args)
{
Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("StackOverflow");
shell.setLayout(new GridLayout(2, false));
Composite myComposite = new Composite(shell, SWT.NONE);
myComposite.setLayout(new GridLayout(1, false));
myComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
Composite deComposite = new Composite(myComposite, SWT.NONE);
deComposite.setLayout(new RowLayout(SWT.HORIZONTAL));
Label createDetailsde = createDetailsde(deComposite);
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
private static Label createDetailsde(Composite detailsComposite)
{
Label linkLabel = new Label(detailsComposite, SWT.NONE);
linkLabel.setText("test");
return linkLabel;
}
答案 1 :(得分:0)
deGridLayout.numColumns = 1;
如果您有一列,则每个元素都会开始一个新行。定义一行GridLayout。
答案 2 :(得分:0)
错误是你已经定义了一个带有1列的GridLayout
GridLayout deGridLayout = new GridLayout();
deGridLayout.numColumns = 1;
deComposite.setLayout(deGridLayout);
因此,每当您向面板“添加”一个组件时,您将填充当前行并将进入下一行。除此之外,你对GridLayout的使用相当奇怪。使用构造函数并定义它的行为而不是访问numColumns。
GridLayout deGridLayout = new GridLayout(10, 1); //Room for 10 components in one row.
deComposite.setLayout(deGridLayout);
这里的问题是;您的行中需要多少个组件(列)?