作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我创建了几个链接按钮。
我希望所有按钮都在同一行。
每次我插入新按钮时,它都会进入下一行。
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 cannot be cast to org.eclipse.swt.layout.RowData
最佳答案
您使用的 GridLayout
只有一列 (deGridLayout.numColumns = 1;
)。您可以做两件事:
GridLayout
以包含您想要的 Button
的列数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();
}
这是它的样子:
您的代码存在的问题是您试图使用 RowLayout
将 GridData
添加到 Composite
。那是不可能的。您的代码可以像这样修复(并大量压缩):
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;
}
关于java - 如何将所有按钮放在同一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21879093/
我是一名优秀的程序员,十分优秀!