用SWT做一个,把常见类型控件都展示在界面上的窗体
时间: 2024-03-16 09:42:00 浏览: 11
好的,我理解了你的问题。下面是使用 SWT 实现的代码,可以展示常见的类型控件:
```java
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;
public class SWTControlDemo {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("SWT Control Demo");
shell.setLayout(new GridLayout(2, false));
// Label
Label label = new Label(shell, SWT.NONE);
label.setText("Label:");
Text labelText = new Text(shell, SWT.BORDER);
labelText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
// Text
Label textLabel = new Label(shell, SWT.NONE);
textLabel.setText("Text:");
Text text = new Text(shell, SWT.BORDER);
text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
// Button
Button button = new Button(shell, SWT.PUSH);
button.setText("Button");
// Combo
Label comboLabel = new Label(shell, SWT.NONE);
comboLabel.setText("Combo:");
Combo combo = new Combo(shell, SWT.READ_ONLY);
combo.add("Item 1");
combo.add("Item 2");
combo.add("Item 3");
combo.select(0);
// List
Label listLabel = new Label(shell, SWT.NONE);
listLabel.setText("List:");
List list = new List(shell, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
list.add("Item 1");
list.add("Item 2");
list.add("Item 3");
GridData listData = new GridData(SWT.FILL, SWT.FILL, true, true);
listData.heightHint = 100;
list.setLayoutData(listData);
// Table
Label tableLabel = new Label(shell, SWT.NONE);
tableLabel.setText("Table:");
Table table = new Table(shell, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL | SWT.FULL_SELECTION);
table.setHeaderVisible(true);
table.setLinesVisible(true);
TableColumn column1 = new TableColumn(table, SWT.NONE);
column1.setText("Column 1");
column1.setWidth(100);
TableColumn column2 = new TableColumn(table, SWT.NONE);
column2.setText("Column 2");
column2.setWidth(100);
TableItem item1 = new TableItem(table, SWT.NONE);
item1.setText(new String[] {"Item 1 Column 1", "Item 1 Column 2"});
TableItem item2 = new TableItem(table, SWT.NONE);
item2.setText(new String[] {"Item 2 Column 1", "Item 2 Column 2"});
GridData tableData = new GridData(SWT.FILL, SWT.FILL, true, true);
tableData.heightHint = 100;
table.setLayoutData(tableData);
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
```
这个窗体包括了常见的几种类型控件,分别是 Label、Text、Button、Combo、List、Table。你可以将这段代码复制到一个 Java 项目中,运行起来查看效果。