学生成绩管理系统(三)

        这个程序,越改,越觉得有问题,就将就着改改吧大哭

3.4 添加学生模块

先看一下界面,主界面没有修改完,修改完之后在贴代码:


学生成绩管理系统(三)_第1张图片

图1 添加学生界面

package org.ygy.view;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import org.ygy.exception.ServiceException;
import org.ygy.model.Student;
import org.ygy.model.StudentClass;
import org.ygy.service.IStuClassService;
import org.ygy.service.IStudentService;
import org.ygy.service.impl.StuClassService;
import org.ygy.service.impl.StudentService;

/**
 * 添加学生
 * 
 * @author Administrator
 *
 */
public class AddStudentPanel extends JPanel {
	private static final long serialVersionUID = 1L;
	
	private JLabel nameLabel = null ;
	private JLabel genderLabel = null ;
	private JLabel stuClassLabel = null;
	
	private JTextField nameText = null;
	private ButtonGroup genderGroup = null;
	private JRadioButton male = null;
	private JRadioButton female = null;
	
	private JButton button_save = null;
	private JButton button_reset = null;
	private JComboBox stuClassComboBox = null;
	
	private List<StudentClass> classList = null;
	
	public AddStudentPanel() {
		setLayout(new GridLayout(5, 2 , 5 , 5));
		
		nameLabel = new JLabel("姓名:" , JLabel.CENTER);
		genderLabel = new JLabel("性别:" , JLabel.CENTER);
		stuClassLabel = new JLabel("班级:" , JLabel.CENTER);
		nameText = new JTextField(20);
		
		add(nameLabel);
		add(nameText);
		add(genderLabel);
		initialGender();
		add(stuClassLabel);

		initialComboBox();
		initialButton();
	}
	
	/**
	 * 初始化按钮
	 */
	private void initialButton() {
		button_save = new JButton("保存");
		button_save.setBounds(60 , 0 , 130, 32);
		button_reset = new JButton("重置");
		button_reset.setBounds(30 , 0 , 130, 32);
		
		button_save.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				//验证输入信息
				String name = nameText.getText().trim();
				boolean gender = male.isSelected() ? true : false;
				
				//得到的是班级名称,应得到对应的班级ID
				int stuClassId = findClassId(stuClassComboBox.getSelectedItem().toString().trim());
				
				if((!"".equals(name)) && (name != null)) {
					Student student = new Student();
					//得到输入的学生信息
					student.setName(name);
					student.setGender(gender);
					student.setStudentClass(new StudentClass(stuClassId));
					
					IStudentService studentService = new StudentService();
					try {
						studentService.addStudent(student);
						display("添加成功!" , "提示");
					} catch (ServiceException e1) {
						display(e1.getMessage() , "错误提示");
					}
					clearInput();
				} else {
					display("姓名不能为空!" , "错误提示");
				}
			}
		});
		
		button_reset.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				clearInput();
			}
		});
		
		add(new JPanel().add(button_save));
		add(new JPanel().add(button_reset));
	}
	
	private int findClassId(String name) {
		int id = 0;
		
		for(StudentClass each : classList) {
			if(each.getName().equals(name)) {
				id = each.getId();
				break;
			}
		}
		
		return id;
	}
	
	/**
	 * 清空输入信息
	 */
	private void clearInput() {
		nameText.setText("");
	}
	
	private void display(String message , String title) {
		JOptionPane.showMessageDialog(this ,  message , title , JOptionPane.INFORMATION_MESSAGE);
	}
	/**
	 * 初始化班级下拉框
	 */
	private void initialComboBox() {
		IStuClassService stuClassService = new StuClassService();
		try {
			classList = stuClassService.getClassList();
		} catch (ServiceException e1) {
			display(e1.getMessage() , "错误提示");
		}
		
		String[] stuClass = new String[classList.size()];
		for(int c=0; c<stuClass.length; c++) {
			stuClass[c] = classList.get(c).getName();
		}
		
		stuClassComboBox = new JComboBox(stuClass);
		add(stuClassComboBox);
	}
	
	/**
	 * 初始化性别单选按钮
	 */
	private void initialGender() {
		genderGroup = new ButtonGroup();
		male = new JRadioButton("男");
		male.setSelected(true);
		female = new JRadioButton("女");
		genderGroup.add(male);
		genderGroup.add(female);
		
		JPanel genderPanel = new JPanel();
		genderPanel.add(male);
		genderPanel.add(female);
		
		add(genderPanel);
	}
}

嗯,我想了想,还是不在这里接着写了,把修改完之后的代码上传,希望对初学的同学有帮助。

你可能感兴趣的:(学生成绩管理系统(三))