Java SWT 提供了四种标准布局类:FillLayout
、RowLayout
、GridLayout
、FormLayout
。我知道如何使用每个布局类,但在组合布局类时遇到困难。
例如,我的应用程序中有一个使用 GridLayout
的屏幕。我想添加两个垂直(即 FillLayout
)布局;一个布局位于 GridLayout
左侧,另一个布局位于 GridLayout
右侧。不幸的是,我不知道如何获得我想要的整体布局(即见底部的 ascii 艺术)。
下面的 SO 链接给出了示例答案,但我想通过添加 FormLayout 来避免过多更改代码。
can I combine SWT GridLayout and FillLayout
是否有示例代码允许我在不使用 FormLayout 的情况下将两个 FillLayout 添加到网格布局?应如下所示(忽略最右侧 FillLayout 中粘贴错误的 ascii)。
+-------------+ +-------------------------------------------+ +--------------+
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| Fill Layout| | Grid Layout | | Fill Layout |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
+-------------+ +-------------------------------------------+ +--------------+
最佳答案
尝试下面的代码,如果您看起来与此相似,请告诉我。
我正在为 shell 使用 Absolute Layout
,我在我的应用程序中创建了 3 个复合体,并为 2 个复合体应用了 Fill Layout
和 Grid Layout
对于 1 个复合 Material 。引用下面的代码
public class SampleWindow {
protected Shell shell;
/**
* Launch the application.
* @param args
*/
public static void main(String[] args) {
try {
SampleWindow window = new SampleWindow();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Create contents of the window.
*/
protected void createContents() {
shell = new Shell();
shell.setSize(531, 363);
shell.setText("SWT Application");
Composite composite = new Composite(shell, SWT.BORDER);
composite.setBounds(10, 10, 131, 304);
composite.setLayout(new FillLayout(SWT.HORIZONTAL));
Label lblNewLabel = new Label(composite, SWT.NONE);
lblNewLabel.setText("Fill Layout");
Composite composite_1 = new Composite(shell, SWT.BORDER);
composite_1.setBounds(159, 10, 199, 304);
composite_1.setLayout(new GridLayout(3, false));
Label lblNewLabel_1 = new Label(composite_1, SWT.NONE);
lblNewLabel_1.setText("Grid Layout");
Composite composite_2 = new Composite(shell, SWT.BORDER);
composite_2.setBounds(382, 10, 123, 304);
composite_2.setLayout(new FillLayout(SWT.HORIZONTAL));
Label lblNewLabel_2 = new Label(composite_2, SWT.NONE);
lblNewLabel_2.setText("Fill Layout");
}
}
输出将类似于以下内容:
关于java - 在不添加 FormLayout 的情况下组合 GridLayout 和 FillLayout?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50740543/