gpt4 book ai didi

java - 如何将所有按钮放在同一行?

转载 作者:行者123 更新时间:2023-11-29 03:24:17 24 4
gpt4 key购买 nike

我创建了几个链接按钮。

我希望所有按钮都在同一行。

每次我插入新按钮时,它都会进入下一行。

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;)。您可以做两件事:

  1. 定义 GridLayout 以包含您想要的 Button 的列数
  2. 改用 RowLayoutSWT.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();
}

这是它的样子:

enter image description here


您的代码存在的问题是您试图使用 RowLayoutGridData 添加到 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/

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