用Java写一个SWT类-应用程序窗口班级姓名学号的代码
时间: 2023-12-10 21:38:54 浏览: 117
下面是一个使用 SWT 库编写的应用程序窗口代码,其中包括班级、姓名和学号的文本框:
```java
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
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.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class ApplicationWindow {
private Text textClass;
private Text textName;
private Text textNumber;
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("Application Window");
shell.setLayout(new GridLayout(2, false));
// 添加班级标签和文本框
Label labelClass = new Label(shell, SWT.NONE);
labelClass.setText("Class:");
textClass = new Text(shell, SWT.BORDER);
textClass.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
// 添加姓名标签和文本框
Label labelName = new Label(shell, SWT.NONE);
labelName.setText("Name:");
textName = new Text(shell, SWT.BORDER);
textName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
// 添加学号标签和文本框
Label labelNumber = new Label(shell, SWT.NONE);
labelNumber.setText("Number:");
textNumber = new Text(shell, SWT.BORDER);
textNumber.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
// 添加确认按钮
Button button = new Button(shell, SWT.PUSH);
button.setText("OK");
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
String classStr = textClass.getText();
String nameStr = textName.getText();
String numberStr = textNumber.getText();
System.out.println("Class: " + classStr + ", Name: " + nameStr + ", Number: " + numberStr);
}
});
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
```
上述代码创建了一个基本的 SWT 应用程序窗口,其中包括班级、姓名和学号的文本框以及一个确认按钮。在单击确认按钮后,将在控制台上显示输入文本框中的值。
阅读全文