简体   繁体   English

网格布局中的间距问题

[英]spacing issue in gridlayout

I am facing a small issue in gridlayout. 我在gridlayout中面临一个小问题。 I have one one label and text in one row and another label in the next row. 我在一行中有一个标签和文本,在下一行中有另一标签。 the label in the second row having more length than the label in the first label in the first row. 第二行中的标签比第一行中的第一标签具有更长的长度。 Now I want to position the text field in the first row near to the label1. 现在,我要在第一行中将文本字段放置在label1附近。 Since the labe2 has higher characters than label1 it takes the column length of label2. 由于labe2具有比label1高的字符,因此它占用了label2的列长。 due to that the textfield doesnt come near to the label1. 由于文本字段不靠近label1。 Is it possible to make the textfield near to the label? 是否可以使文本字段靠近标签?

Please find the code below.. 请在下面找到代码。

package zx.example.hello;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

    public class GridLayoutExample {

        public static void main(String[] args) {
            Display display = new Display();
            Shell shell = new Shell(display);
            GridLayout gridLayout = new GridLayout();
            gridLayout.numColumns = 2;
            shell.setLayout(gridLayout);

            GridData data1 = new GridData();
            Label label1 = new Label(shell, SWT.PUSH);
            label1.setText("Label1");
            label1.setLayoutData(data1);

            Text text1 = new Text(shell, SWT.BORDER);
            text1.setText("");

            Label label2 = new Label(shell, SWT.PUSH);
            label2.setText("Label 222222222222222222222222222222");
            /*Text text2 = new Text(shell, SWT.BORDER);
            text2.setText("B4");*/

            shell.pack();
            shell.open();

            while (!shell.isDisposed()) {
                if (!display.readAndDispatch())
                    display.sleep();
            }
        }
    }

Yes you can, add this line: 是的,您可以添加以下行:

label2.setLayoutData(new GridData(SWT.BEGINNING, SWT.TOP, true, true, 2, 1));

It will make the second Label stretch across two columns of the GridLayout . 它将使第二个Label跨越GridLayout两列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM