用JAVA创建一个具体的学生管理程序代码带界面和mvc架构
时间: 2023-06-24 19:06:02 浏览: 122
这里提供一个简单的学生管理程序的代码示例,包含界面和MVC架构。需要注意的是,这个示例只是为了展示MVC架构的基本思想和界面设计,实际项目中可能需要更多的功能和细节处理。
1. Model类
```java
public class StudentModel {
private String name;
private int age;
private String gender;
public StudentModel(String name, int age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
```
2. View类
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
public class StudentView extends JFrame {
private JLabel nameLabel;
private JTextField nameTextField;
private JLabel ageLabel;
private JTextField ageTextField;
private JLabel genderLabel;
private JComboBox genderComboBox;
private JButton addButton;
private JButton deleteButton;
private JTable studentTable;
public StudentView() {
initUI();
}
private void initUI() {
setTitle("Student Management System");
setSize(500, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
nameLabel = new JLabel("Name:");
nameTextField = new JTextField(10);
ageLabel = new JLabel("Age:");
ageTextField = new JTextField(10);
genderLabel = new JLabel("Gender:");
genderComboBox = new JComboBox(new String[]{"Male", "Female"});
addButton = new JButton("Add");
deleteButton = new JButton("Delete");
studentTable = new JTable();
JPanel topPanel = new JPanel();
topPanel.setLayout(new FlowLayout());
topPanel.add(nameLabel);
topPanel.add(nameTextField);
topPanel.add(ageLabel);
topPanel.add(ageTextField);
topPanel.add(genderLabel);
topPanel.add(genderComboBox);
topPanel.add(addButton);
topPanel.add(deleteButton);
JPanel bottomPanel = new JPanel();
bottomPanel.setLayout(new BorderLayout());
bottomPanel.add(studentTable.getTableHeader(), BorderLayout.NORTH);
bottomPanel.add(studentTable, BorderLayout.CENTER);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(topPanel, BorderLayout.NORTH);
getContentPane().add(bottomPanel, BorderLayout.CENTER);
}
public String getName() {
return nameTextField.getText();
}
public int getAge() {
return Integer.parseInt(ageTextField.getText());
}
public String getGender() {
return (String) genderComboBox.getSelectedItem();
}
public void setStudentTableData(List<StudentModel> dataList) {
Object[][] data = new Object[dataList.size()][3];
for (int i = 0; i < dataList.size(); i++) {
data[i][0] = dataList.get(i).getName();
data[i][1] = dataList.get(i).getAge();
data[i][2] = dataList.get(i).getGender();
}
studentTable.setModel(new DefaultTableModel(data, new String[]{"Name", "Age", "Gender"}));
}
public void addAddButtonListener(ActionListener listener) {
addButton.addActionListener(listener);
}
public void addDeleteButtonListener(ActionListener listener) {
deleteButton.addActionListener(listener);
}
}
```
3. Controller类
```java
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
public class StudentController {
private StudentView view;
private List<StudentModel> dataList;
public StudentController(StudentView view) {
this.view = view;
this.dataList = new ArrayList<>();
this.view.setStudentTableData(this.dataList);
this.view.addAddButtonListener(new AddButtonListener());
this.view.addDeleteButtonListener(new DeleteButtonListener());
}
class AddButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String name = view.getName();
int age = view.getAge();
String gender = view.getGender();
StudentModel student = new StudentModel(name, age, gender);
dataList.add(student);
view.setStudentTableData(dataList);
}
}
class DeleteButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
int[] rows = view.studentTable.getSelectedRows();
for (int i = rows.length - 1; i >= 0; i--) {
dataList.remove(rows[i]);
}
view.setStudentTableData(dataList);
}
}
public static void main(String[] args) {
StudentView view = new StudentView();
StudentController controller = new StudentController(view);
view.setVisible(true);
}
}
```
在MVC架构中,Model类保存数据,View类负责显示界面,Controller类负责控制View和Model之间的交互。
这个示例中,StudentModel类用于保存学生的姓名、年龄和性别。StudentView类是界面的实现,包括文本框、下拉框、按钮和表格等控件。StudentController类是控制器,处理按钮事件、更新数据和刷新界面等操作。
在main方法中,创建View和Controller对象,并显示界面。运行程序后,可以添加学生信息并在表格上显示,也可以删除选定的行。
阅读全文