使用Java创建一个SWT项目。在它里面创建一个可视化的SWT类-应用程序窗口。 在窗口上添加每个项目后创建的源代码。 使应用程序窗口有意义。添加一些标签、文本框、按钮,并打印出一张简单的学生信息表,写出完整代码
时间: 2024-02-25 10:52:00 浏览: 18
好的,以下是使用Java创建一个SWT项目,在窗口上添加标签、文本框、按钮,以及打印出学生信息表的完整代码:
```java
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
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 SWTApplication {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("学生信息表");
shell.setLayout(new FillLayout());
// 创建一个网格布局,设置每一行有两列
GridLayout layout = new GridLayout(2, false);
shell.setLayout(layout);
// 添加标签和文本框
Label nameLabel = new Label(shell, SWT.NONE);
nameLabel.setText("姓名:");
Text nameText = new Text(shell, SWT.SINGLE | SWT.BORDER);
nameText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
Label ageLabel = new Label(shell, SWT.NONE);
ageLabel.setText("年龄:");
Text ageText = new Text(shell, SWT.SINGLE | SWT.BORDER);
ageText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
Label genderLabel = new Label(shell, SWT.NONE);
genderLabel.setText("性别:");
Text genderText = new Text(shell, SWT.SINGLE | SWT.BORDER);
genderText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
// 添加按钮
Button printButton = new Button(shell, SWT.PUSH);
printButton.setText("打印学生信息表");
printButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
GC gc = new GC(shell);
Image image = new Image(display, shell.getBounds());
gc.copyArea(image, 0, 0);
gc.dispose();
// 打印学生信息表
System.out.println("姓名:" + nameText.getText());
System.out.println("年龄:" + ageText.getText());
System.out.println("性别:" + genderText.getText());
}
});
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
```
这个程序创建了一个名为 "学生信息表" 的窗口,有三个文本框用于输入学生的姓名、年龄和性别,还有一个按钮用于打印出学生信息表。在点击按钮后,程序会在控制台上打印出输入的学生信息。