gpt4 book ai didi

java - SWT 文本组件不会水平填充

转载 作者:行者123 更新时间:2023-12-01 16:32:48 25 4
gpt4 key购买 nike

我与 SWT 合作,在按照我想要的方式布局时遇到一些问题。本质上,我有一个 Text 对象,我希望它的宽度是它所在组件的宽度。我可以使用 FillLayout 但下面的按钮也会变大,这不是我想要的。

这是说明问题的屏幕截图:
enter image description here

这是我用来重现的测试代码,并尝试解决问题:

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();
}
}
}

我认为这只是我没有在 inputFieldGridData 对象上正确设置某些内容,但无论它是什么,我肯定没有看到它。

最佳答案

您必须使用setLayoutData()来设置GridData而不是setData():

Text inputField = new Text(shell, SWT.BORDER);
GridData data = new GridData();
data.horizontalAlignment = SWT.FILL;
data.grabExcessHorizontalSpace = true;
inputField.setLayoutData(data);

顺便说一句:使用 GridData.FILL is not recommended ,请改用 SWT.FILL

关于java - SWT 文本组件不会水平填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12715886/

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