使用Eclipse创建一个SWT工程,设计一个学生管理系统,包括登录界面,注册界面和主界面。实现登陆界面登陆功能,用户身份验证(假设用户名为test,密码为123)通过进入主界面,失败提醒错误信息
时间: 2023-08-05 14:02:43 浏览: 289
好的,以下是实现步骤:
1. 打开Eclipse,选择File -> New -> Project,选择Java Project,输入项目名称并点击Next。
2. 在下一个页面中选择“Create project from template”,选择“SWT Project”,然后点击“Finish”。
3. 在src文件夹下创建三个类文件:LoginWindow.java、RegisterWindow.java和MainWindow.java。
4. 在LoginWindow.java中,创建一个登录界面,包括用户名输入框、密码输入框和登录按钮。实现登录功能,当用户名为“test”,密码为“123”时,打开主界面MainWindow;否则,提示错误信息。
5. 在RegisterWindow.java中,创建一个注册界面,包括用户名输入框、密码输入框和注册按钮。实现注册功能,将注册信息保存到数据库中。
6. 在MainWindow.java中,创建一个主界面,包括学生信息列表和操作按钮,如添加、修改、删除等。
代码示例:
LoginWindow.java
```java
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
public class LoginWindow {
private Display display;
private Shell shell;
private Text txtUsername;
private Text txtPassword;
public LoginWindow() {
this.display = new Display();
this.shell = new Shell(display);
this.shell.setText("Login");
GridLayout layout = new GridLayout(2, false);
this.shell.setLayout(layout);
Label lblUsername = new Label(this.shell, SWT.NONE);
lblUsername.setText("Username");
txtUsername = new Text(this.shell, SWT.BORDER);
Label lblPassword = new Label(this.shell, SWT.NONE);
lblPassword.setText("Password");
txtPassword = new Text(this.shell, SWT.BORDER | SWT.PASSWORD);
Button btnLogin = new Button(this.shell, SWT.NONE);
btnLogin.setText("Login");
btnLogin.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
if (txtUsername.getText().equals("test") && txtPassword.getText().equals("123")) {
MessageBox messageBox = new MessageBox(shell, SWT.NONE);
messageBox.setMessage("Login successful!");
messageBox.open();
new MainWindow();
shell.dispose();
} else {
MessageBox messageBox = new MessageBox(shell, SWT.NONE);
messageBox.setMessage("Invalid username or password!");
messageBox.open();
}
}
});
Button btnRegister = new Button(this.shell, SWT.NONE);
btnRegister.setText("Register");
btnRegister.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
new RegisterWindow();
}
});
GridData gd = new GridData(SWT.FILL, SWT.CENTER, true, false);
txtUsername.setLayoutData(gd);
txtPassword.setLayoutData(gd);
gd = new GridData(SWT.CENTER, SWT.CENTER, true, false, 2, 1);
btnLogin.setLayoutData(gd);
btnRegister.setLayoutData(gd);
this.shell.pack();
this.shell.open();
while (!this.shell.isDisposed()) {
if (!this.display.readAndDispatch())
this.display.sleep();
}
this.display.dispose();
}
public static void main(String[] args) {
new LoginWindow();
}
}
```
RegisterWindow.java
```java
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
public class RegisterWindow {
private Display display;
private Shell shell;
private Text txtUsername;
private Text txtPassword;
public RegisterWindow() {
this.display = new Display();
this.shell = new Shell(display);
this.shell.setText("Register");
GridLayout layout = new GridLayout(2, false);
this.shell.setLayout(layout);
Label lblUsername = new Label(this.shell, SWT.NONE);
lblUsername.setText("Username");
txtUsername = new Text(this.shell, SWT.BORDER);
Label lblPassword = new Label(this.shell, SWT.NONE);
lblPassword.setText("Password");
txtPassword = new Text(this.shell, SWT.BORDER | SWT.PASSWORD);
Button btnRegister = new Button(this.shell, SWT.NONE);
btnRegister.setText("Register");
btnRegister.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
// TODO: Save registration information to database
MessageBox messageBox = new MessageBox(shell, SWT.NONE);
messageBox.setMessage("Registration successful!");
messageBox.open();
shell.dispose();
}
});
GridData gd = new GridData(SWT.FILL, SWT.CENTER, true, false);
txtUsername.setLayoutData(gd);
txtPassword.setLayoutData(gd);
gd = new GridData(SWT.CENTER, SWT.CENTER, true, false, 2, 1);
btnRegister.setLayoutData(gd);
this.shell.pack();
this.shell.open();
while (!this.shell.isDisposed()) {
if (!this.display.readAndDispatch())
this.display.sleep();
}
this.display.dispose();
}
}
```
MainWindow.java
```java
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
public class MainWindow {
private Display display;
private Shell shell;
public MainWindow() {
this.display = new Display();
this.shell = new Shell(display);
this.shell.setText("Student Management System");
GridLayout layout = new GridLayout(2, false);
this.shell.setLayout(layout);
Table table = new Table(this.shell, SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION);
table.setLinesVisible(true);
table.setHeaderVisible(true);
TableColumn col1 = new TableColumn(table, SWT.NONE);
col1.setText("ID");
col1.setWidth(50);
TableColumn col2 = new TableColumn(table, SWT.NONE);
col2.setText("Name");
col2.setWidth(100);
TableColumn col3 = new TableColumn(table, SWT.NONE);
col3.setText("Gender");
col3.setWidth(50);
TableColumn col4 = new TableColumn(table, SWT.NONE);
col4.setText("Age");
col4.setWidth(50);
TableColumn col5 = new TableColumn(table, SWT.NONE);
col5.setText("Major");
col5.setWidth(150);
for (int i = 0; i < 10; i++) {
TableItem item = new TableItem(table, SWT.NONE);
item.setText(new String[] { Integer.toString(i), "Student " + i, "M", Integer.toString(20 + i), "Computer Science" });
}
Button btnAdd = new Button(this.shell, SWT.NONE);
btnAdd.setText("Add");
btnAdd.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
// TODO: Show add student dialog
MessageBox messageBox = new MessageBox(shell, SWT.NONE);
messageBox.setMessage("Add student");
messageBox.open();
}
});
Button btnEdit = new Button(this.shell, SWT.NONE);
btnEdit.setText("Edit");
btnEdit.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
// TODO: Show edit student dialog
MessageBox messageBox = new MessageBox(shell, SWT.NONE);
messageBox.setMessage("Edit student");
messageBox.open();
}
});
Button btnDelete = new Button(this.shell, SWT.NONE);
btnDelete.setText("Delete");
btnDelete.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
// TODO: Delete selected student(s)
MessageBox messageBox = new MessageBox(shell, SWT.NONE);
messageBox.setMessage("Delete student");
messageBox.open();
}
});
GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
gd.widthHint = 500;
gd.heightHint = 300;
table.setLayoutData(gd);
gd = new GridData(SWT.CENTER, SWT.CENTER, true, false, 2, 1);
btnAdd.setLayoutData(gd);
btnEdit.setLayoutData(gd);
btnDelete.setLayoutData(gd);
this.shell.pack();
this.shell.open();
while (!this.shell.isDisposed()) {
if (!this.display.readAndDispatch())
this.display.sleep();
}
this.display.dispose();
}
}
```
以上是一个简单的学生管理系统的实现,其中登录界面和主界面使用了SWT库实现。注册界面只是一个示例,具体的实现需要根据实际情况进行修改。