我正在使用 GridLayout 为 RCP 应用程序创建 GUI,但我遇到了问题,因为据我了解,GridLayout 按照您的顺序添加元素创建它们/设置它们的布局数据?因此,这并没有提供太多/任何灵 active 来移动元素和添加新元素,同时保持与先前放置的元素相同的布局。
我应该使用其他方法来制作 GUI 吗?我需要它灵活且易于指定各个组件的大小和位置,而不是指定下一个空间中的组件。
目前GridLayout
仅允许我指定屏幕上的列数,然后指定元素属性(SWT.LEFT、SWT.CENTER
)等。
这是我的代码来说明我的意思:
import javax.annotation.PostConstruct;
import org.eclipse.swt.*;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
public class CreateUI {
//Text area for the variables
Text variableArea;
Text methodArea;
Text otherArea;
Text fooArea;
@PostConstruct
//Annotation indicates the method is to be called after the class is constructed
public void createInterface(Composite parent){
int columns =4;
//Set the layout of the parent (the Part) to a grid layout
parent.setLayout(new GridLayout(columns, true));
variableArea = new Text(parent, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
variableArea.setText("Hello");
variableArea.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, true, 1, 1));
methodArea = new Text(parent, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
methodArea.setText("Hello2");
methodArea.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, true, 1, 1));
otherArea = new Text(parent, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
otherArea.setText("Hello3");
otherArea.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, true, 1, 1));
fooArea = new Text(parent, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
fooArea.setText("Hello4");
fooArea.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, true, 1, 1));
}
}
这会产生这样的结果:
目前只有这 4 个文本区域作为示例,但稍后我将拥有多个文本区域、按钮、下拉菜单等,我希望能够更轻松地设置它们的格式并自由放置它们。
对于上面的示例,我希望能够将这 4 个文本区域放置在一列中,以显示信息,并将其他元素自由放置在剩余空间中。
任何指针对于我应该使用的东西都是很好的。
最佳答案
MigLayout
是相当灵活的。从内置布局来看,FormLayout
也是一个不错的选择,但它比 GridLayout
更难实现,所以我只建议您在发现有问题时使用它GridLayout
确实不适合。
关于java - RCP GUI 使用什么类型的布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15008756/