Java 如何将所有按钮放在同一行?
我创建了几个链接按钮 我希望所有的按钮都在同一行 每次我插入新按钮,它就会进入下一行Java 如何将所有按钮放在同一行?,java,swt,Java,Swt,我创建了几个链接按钮 我希望所有的按钮都在同一行 每次我插入新按钮,它就会进入下一行 public void createControls(Composite parent) { myComposite = new Composite(parent, SWT.NONE); GridLayout detailsSideGridLayout = new GridLayout(); detailsSideGridLayout.numColumns = 1; myCom
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
deGridLayout.numColumns = 1;
如果有一列,则每个元素将开始一个新行。使用一行定义GridLayout
如果有一列,则每个元素将开始一个新行。定义一行的GridLayout。错误在于定义了一列的GridLayout
GridLayout deGridLayout = new GridLayout();
deGridLayout.numColumns = 1;
deComposite.setLayout(deGridLayout);
因此,每次将组件“添加”到面板时,您都会填充当前行并进入下一行。除此之外,您对GridLayout的使用相当奇怪。使用构造函数并定义其行为,而不是访问numcolumn
GridLayout deGridLayout = new GridLayout(10, 1); //Room for 10 components in one row.
deComposite.setLayout(deGridLayout);
但问题是;行中需要多少组件(列)?错误是您定义了一个包含1列的GridLayout
GridLayout deGridLayout = new GridLayout();
deGridLayout.numColumns = 1;
deComposite.setLayout(deGridLayout);
因此,每次将组件“添加”到面板时,您都会填充当前行并进入下一行。除此之外,您对GridLayout的使用相当奇怪。使用构造函数并定义其行为,而不是访问numcolumn
GridLayout deGridLayout = new GridLayout(10, 1); //Room for 10 components in one row.
deComposite.setLayout(deGridLayout);
但问题是;您的行中需要多少个组件(列?您正在使用的
GridLayout
只有一列(deGridLayout.numColumns=1;
)。您可以做两件事:
GridLayout
,使其具有所需的按钮的列数
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();
}
您正在使用的
GridLayout
只有一列(deGridLayout.numColumns=1;
)。您可以做两件事:
GridLayout
,使其具有所需的按钮的列数
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();
}
未定义构造函数
GridLayout(int,int)
。第二个参数是一个布尔值
,说明列是否应该具有相同的宽度。行数不必指定,它是动态的。@Baz;你确定?网格布局--不是网格包,只是网格。。。是的,我肯定。它不是Swing/AWT。它是SWT而不是AWT/Swing未定义构造函数GridLayout(int,int)
。第二个参数是一个布尔值
,说明列是否应该具有相同的宽度。行数不必指定,它是动态的。@Baz;你确定?网格布局--不是网格包,只是网格。。。是的,我肯定。它不是Swing/AWT。它是SWT而不是AWT/Swing。复合myComposite是GridLayout,然后我得到了转储。我更新了我的code@user1365697“我得到了转储”这是什么意思?我想是因为我有两种类型的布局组合。当detailsComposite.layout()时转储会增加@user1365697什么是“转储”?如果您指的是堆栈跟踪,那么请将其添加到您的问题中。错误是org.eclipse.swt.layout.GridData无法转换为org.eclipse.swt.layout.rowdata复合myComposite是GridLayout,然后我得到了转储。我更新了我的code@user1365697“我得到了转储”这是什么意思?我想是因为我有两种类型的布局组合。当detailsComposite.layout()时转储会增加@user1365697什么是“转储”?如果您指的是堆栈跟踪,请将其添加到问题中。错误为org.eclipse.swt.layout.GridData不能转换为org.eclipse.swt.layout.RowData