使用Java创建一个SWT项目。在它里面创建一个可视化的SWT类-应用程序窗口。 在窗口上添加每个项目后创建的源代码。 使应用程序窗口有意义。添加一些标签、文本框、按钮,并打印出一张简单的学生成绩表,写出完整代码
时间: 2024-02-25 07:51:50 浏览: 25
好的,以下是使用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 mathLabel = new Label(shell, SWT.NONE);
mathLabel.setText("数学成绩:");
Text mathText = new Text(shell, SWT.SINGLE | SWT.BORDER);
mathText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
Label chineseLabel = new Label(shell, SWT.NONE);
chineseLabel.setText("语文成绩:");
Text chineseText = new Text(shell, SWT.SINGLE | SWT.BORDER);
chineseText.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("数学成绩:" + mathText.getText());
System.out.println("语文成绩:" + chineseText.getText());
}
});
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
```
这个程序创建了一个名为 "学生成绩表" 的窗口,有三个文本框用于输入学生的姓名、数学成绩和语文成绩,还有一个按钮用于打印出学生成绩表。在点击按钮后,程序会在控制台上打印出输入的学生信息和成绩。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)