GridLayout(网格布局),通过设定控件的列宽来确定控件的大小和位置,是使用得最多的布局模式。
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) {
Label label;
Text text;
GridData gridData;
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("GridLayout Example");
shell.setBounds(100, 100, 200, 100);
GridLayout layout = new GridLayout();
layout.numColumns = 2;
shell.setLayout(layout);
label = new Label(shell, SWT.LEFT);
label.setText("Enter your first and last name");
gridData = new GridData();
gridData.horizontalSpan = 2;
label.setLayoutData(gridData);
label = new Label(shell, SWT.LEFT);
label.setText("First:");
text = new Text(shell, SWT.SINGLE | SWT.BORDER);
gridData = new GridData();
gridData.horizontalAlignment = GridData.FILL;
gridData.grabExcessHorizontalSpace = true;
text.setLayoutData(gridData);
label = new Label(shell, SWT.LEFT);
label.setText("Last:");
text = new Text(shell, SWT.SINGLE | SWT.BORDER);
gridData = new GridData();
gridData.horizontalAlignment = GridData.FILL;
gridData.grabExcessHorizontalSpace = true;
text.setLayoutData(gridData);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
效果如下:
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于