我与 SWT 合作,在按我想要的方式布置事情时遇到了一些问题。本质上,我有一个Text
对象,我希望它的宽度是它所在的组件的宽度。我可以使用FillLayout
,但它下面的按钮也会变大,这不是我想要的。
这是一个屏幕截图来说明问题:
这是我用来重现的测试代码,并尝试解决问题:
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;
public class Test {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
//setup the layout manager
GridLayout layout = new GridLayout(1, false);
layout.horizontalSpacing = 0;
layout.marginHeight = 0;
layout.verticalSpacing = 5;
layout.marginWidth = 0;
layout.marginBottom = 20;
layout.marginTop = 5;
shell.setLayout(layout);
//create the text field
Text inputField = new Text(shell, SWT.BORDER);
GridData data = new GridData();
data.horizontalAlignment = GridData.FILL;
data.grabExcessHorizontalSpace = true;
inputField.setData(data);
//create the button
Button lookup = new Button(shell, SWT.PUSH);
lookup.setText("Lookup");
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
}
}
我认为这只是我没有在GridData
对象上为 正确设置一些东西inputField
,但不管它是什么,我绝对没有看到它。