학생 관리 시스템을 봉인하다.

38496 단어
Java 기초 지식을 배웠습니다. 여기서 학생 관리 시스템을 봉인 분류 관리를 하고 데이터베이스와 작업할 때 추가, 삭제, 수정, 조회에 문제가 존재합니다. JLable에 제때에 표시하지 못하고 모든 학생들이 위에 표시할 수 있습니다. 필요하면 스스로 수정하고 부족한 점이 있으면 조언을 해 주십시오. 감사합니다!
다음은 코드입니다.
package com에 있습니다.sc.java.managers;두 개의 프로그램이 실행되었습니다.
기본 인터페이스 창:
package com.sc.java.managers;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import com.sc.java.managers.db.TeacherDB;
import com.sc.java.managers.table.StudentTable;

public class ManagerGuiDemo extends JFrame {
	Container container;
	JButton loginBtn;
	JButton registBtn;
	JButton resetbtn;

	JLabel accountLable;
	JLabel passWordLable;
	JLabel imageLable;

	JTextField accountTxt;
	JPasswordField passWordTxt;

	JPanel btnJPanel;
	JPanel imageJpanel;
	JPanel gridJpanel;
	TeacherDB db = TeacherDB.getInstance();

	//      
	public ManagerGuiDemo() {
		super("            ");
		this.setBounds(500, 300, 350, 400);
		container = this.getContentPane();
		container.setLayout(new FlowLayout());
		container.setBackground(Color.white);//        
		this.setResizable(false);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	}

	public void init() {
		//     
		ImageIcon image = new ImageIcon("pic//xuesheng.png");

		loginBtn = new JButton("  ");
		registBtn = new JButton("  ");
		resetbtn = new JButton("  ");

		accountLable = new JLabel("   :");
		passWordLable = new JLabel("      :");
		imageLable = new JLabel(image);

		accountTxt = new JTextField(10);
		passWordTxt = new JPasswordField(10);

		gridJpanel = new JPanel();
		btnJPanel = new JPanel();
		imageJpanel = new JPanel();

		//        
		imageJpanel.setLayout(new FlowLayout());
		imageJpanel.add(imageLable);

		gridJpanel.setLayout(new GridLayout(3, 1));
		gridJpanel.add(accountLable);
		gridJpanel.add(accountTxt);

		gridJpanel.add(passWordLable);
		gridJpanel.add(passWordTxt);

		btnJPanel.add(loginBtn);
		btnJPanel.add(registBtn);
		btnJPanel.add(resetbtn);

		container.add(imageJpanel);
		container.add(gridJpanel);
		container.add(btnJPanel);

		//       
		registBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ManagerGuiDemo.this.setVisible(false);
				new Register().initTeacher();
			}
		});

		//       
		loginBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String account = accountTxt.getText();
				String passWord = passWordTxt.getText();

				if (account.equals("") || passWord.equals("")) {
					JOptionPane.showMessageDialog(null, "        !");
					return;
				}
				boolean flag = db.selectTeacherLogin(account, passWord);
				if (flag) {
					setVisible(false);
					new StudentTable().init();
				} else {
					JOptionPane.showMessageDialog(null, "        !");
					accountTxt.setText("");
					passWordTxt.setText("");
				}
			}
		});

		//     
		resetbtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				accountTxt.setText("");
				passWordTxt.setText("");
				return;
			}
		});

		this.setVisible(true);
	}

	public static void main(String[] args) {
		new ManagerGuiDemo().init();
	}
}

로그인 인터페이스:
package com.sc.java.managers;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

import com.sc.java.managers.bean.TeacherBean;
import com.sc.java.managers.db.TeacherDB;

public class Register extends JFrame implements ActionListener {
	Container container;
	JList list;
	String array[];
	JButton confirmBtn;

	JLabel nameLable;
	JLabel accountLable;
	JLabel passWordLable;
	JLabel imageLable;

	JTextField nameTxt;
	JTextField accountTxt;
	JPasswordField passWordTxt;

	JPanel btnJPanel;
	JPanel imageJpanel;
	JPanel gridJpanel;

	TeacherDB db = TeacherDB.getInstance();

	public Register() {
		super("      ");
		this.setBounds(500, 300, 300, 400);
		container = this.getContentPane();
		container.setLayout(new FlowLayout());
		this.setResizable(false);
	}

	public void initTeacher() {
		ImageIcon image = new ImageIcon("pic//jiaoshi.png");
		confirmBtn = new JButton("  ");

		nameLable = new JLabel("      :");
		accountLable = new JLabel("   :");
		passWordLable = new JLabel("      :");
		imageLable = new JLabel(image);

		nameTxt = new JTextField(10);
		accountTxt = new JTextField(10);
		passWordTxt = new JPasswordField(10);

		btnJPanel = new JPanel();
		gridJpanel = new JPanel();
		imageJpanel = new JPanel();

		imageJpanel.setLayout(new FlowLayout());
		imageJpanel.add(imageLable);

		gridJpanel.setLayout(new GridLayout(4, 3));
		gridJpanel.add(nameLable);
		gridJpanel.add(nameTxt);

		gridJpanel.add(accountLable);
		gridJpanel.add(accountTxt);

		gridJpanel.add(passWordLable);
		gridJpanel.add(passWordTxt);

		btnJPanel.add(confirmBtn);

		container.add(imageJpanel);
		container.add(gridJpanel);
		container.add(btnJPanel);

		confirmBtn.addActionListener(this);

		this.setVisible(true);
	}

	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();
		if (source == confirmBtn) {
			String name = nameTxt.getText();
			String account = accountTxt.getText();
			String passWord = passWordTxt.getText();

			if (name.equals("") || account.equals("") || passWord.equals("")) {
				JOptionPane.showMessageDialog(null, "        !");
				return;
			}

			boolean flag = db.selectTeacherAccount(account);
			if (flag) {
				JOptionPane.showMessageDialog(null, "      !        !");
				nameTxt.setText("");
				accountTxt.setText("");
				passWordTxt.setText("");
				return;

			} else {
				TeacherBean teacher = new TeacherBean(name, account, passWord);
				db.addTeacher(teacher);
				new ManagerGuiDemo().init();
			}
		}

	}

	//      
	public static void main(String[] args) {
		new Register().initTeacher();
	}

}

교사류와 학생류 빈 패키지는 다음과 같다.
교사 Bean:
package com.sc.java.managers.bean;

public class TeacherBean {

	private String name;//       
	private String account;//       
	private String passWord;//       

	public TeacherBean() {

	}

	public TeacherBean(String name, String account, String passWord) {
		this.name = name;
		this.account = account;
		this.passWord = passWord;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAccount() {
		return account;
	}

	public void setAccount(String account) {
		this.account = account;
	}

	public String getPassWord() {
		return passWord;
	}

	public void setPassWord(String passWord) {
		this.passWord = passWord;
	}

}

학생 빈:
package com.sc.java.managers.bean;

public class StudentBean {

	private String name;//       
	private String number;//       
	private String age;//       
	private String sex;//       

	public StudentBean() {

	}

	public StudentBean(String name, String number, String age, String sex) {
		this.name = name;
		this.number = number;
		this.age = age;
		this.sex = sex;
	}

	public StudentBean(String number) {
		this.number = number;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getNumber() {
		return number;
	}

	public void setNumber(String number) {
		this.number = number;
	}

	public String getAge() {
		return age;
	}

	public void setAge(String age) {
		this.age = age;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

}

데이터베이스 연결 분류는 다음과 같습니다.
데이터베이스 연결을 설정하려면:
package com.sc.java.managers.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConnectionDB {
	//      
	private String driver = "com.mysql.jdbc.Driver";
	// URL          mydata
	private String url = "jdbc:mysql://localhost:3306/stumanager";
	// MySQL       
	private String user = "root";
	// MySQL      
	private String password = "a123";

	//     
	protected ConnectionDB() {
		try {
			Class.forName(driver);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	//        
	public Connection getConnection() {
		Connection connect = null;
		try {
			connect = (Connection) DriverManager.getConnection(url, user,
					password);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return connect;
	}

	//     
	public void freeConnection(Connection con) {
		try {
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}

연결 교사 테이블:
package com.sc.java.managers.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.sc.java.managers.bean.TeacherBean;


public class TeacherDB extends ConnectionDB{
	
	//          ,         
	private static TeacherDB USS = null;
	private Connection connect = null;
	
	public static TeacherDB getInstance() {
		if (USS == null) {
			USS = new TeacherDB();
		}
		return USS;
	}


	//     
	public void createDatabase(){
		Statement s;
		try {
			s = (Statement) connect.createStatement();
			//      
			String str0 = "create database stumanagers CHARSET=utf8";
			s.execute(str0);
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}
	
	//     
	public void createDataTablesTeacher(){
		try {
			Statement s= (Statement) connect.createStatement();
			//      
			String setDate = "use stumanagers";
			s.execute(setDate);
			
			//        
			String tch = "CREATE TABLE teacher(  "
					+ "id int(2) NOT NULL AUTO_INCREMENT,  "
					+ "name varchar(3) DEFAULT NULL,account varchar(10) DEFAULT NULL,"
					+ "password varchar(10) DEFAULT NULL,PRIMARY KEY (id)"
					+ ") ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8";
			s.execute(tch);
			
		} catch (SQLException e) {
			e.printStackTrace();
		}

	} 

	//             
	public void addTeacher(TeacherBean teacher) {
		Connection con = null;
		PreparedStatement ps = null;
		String sql = "insert into teacher(name,account,passWord)values(?,?,?)";
		try {
			con = getConnection();
			ps = con.prepareStatement(sql);
			ps.setString(1, teacher.getName());
			ps.setString(2, teacher.getAccount());
			ps.setString(3, teacher.getPassWord());
			ps.executeUpdate();
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			try {
				if (ps != null)
					ps.close();
			} catch (SQLException sqle) {
			}
			if (con != null)
				freeConnection(con);
		}
	}

	//              
	public boolean selectTeacherAccount(String account) {
		Connection con = null;
		PreparedStatement ps = null;
		String sql = "select * from teacher where account=?";
		boolean fals = false;
		try {
			con = getConnection();
			ps = con.prepareStatement(sql);
			ps.setString(1, account);
			ResultSet rs = ps.executeQuery();
			if (rs.next()) {
				fals = true;
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			try {
				if (ps != null)
					ps.close();
			} catch (SQLException sqle) {
			}
			if (con != null)
				freeConnection(con);
		}
		return fals;
	}

	//                
	public boolean selectTeacherLogin(String account, String passWord) {
		Connection con = null;
		PreparedStatement ps = null;
		String sql = "select * from teacher where (account,passWord)=(?,?)";
		boolean fals = false;
		try {
			con = getConnection();
			ps = con.prepareStatement(sql);
			ps.setString(1, account);
			ps.setString(2, passWord);
			ResultSet rs = ps.executeQuery();
			if (rs.next()) {
				fals = true;
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			try {
				if (ps != null)
					ps.close();
			} catch (SQLException sqle) {
			}
			if (con != null)
				freeConnection(con);
		}
		return fals;
	}


}

학생표 연결:
package com.sc.java.managers.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.sc.java.managers.bean.StudentBean;


public class StudentDB extends ConnectionDB {
	//          ,         
	private static StudentDB USS = null;

	public static StudentDB getInstance() {
		if (USS == null) {
			USS = new StudentDB();
		}
		return USS;
	}

	//           
	public void addStudent(StudentBean student) {
		Connection con = null;
		PreparedStatement ps = null;
		String sql = "insert into student(name,number,age,sex)values(?,?,?,?)";
		try {
			con = getConnection();
			ps = con.prepareStatement(sql);
			ps.setString(1, student.getName());
			ps.setString(2, student.getNumber());
			ps.setString(3, student.getAge());
			ps.setString(4, student.getSex());
			ps.executeUpdate();
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			try {
				if (ps != null)
					ps.close();
			} catch (SQLException sqle) {
			}
			if (con != null)
				freeConnection(con);
		}
	}

	//               
	public boolean selectStudentByNumber(String number) {
		Connection con = null;
		PreparedStatement ps = null;
		String sql = "select * from student where number=?";
		boolean fals = false;
		try {
			con = getConnection();
			ps = con.prepareStatement(sql);
			ps.setString(1, number);
			ResultSet rs = ps.executeQuery();
			if (rs.next()) {
				fals = true;
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			try {
				if (ps != null)
					ps.close();
			} catch (SQLException sqle) {
			}
			if (con != null)
				freeConnection(con);
		}
		return fals;
	}

	//       
	public StudentBean updateStudentBean(StudentBean student, String oldNumber) {
		Connection con = null;
		PreparedStatement ps = null;
		String sql = "update student set name=?,number=?,age=?,sex=? where number=?";
		try {
			con = getConnection();
			ps = con.prepareStatement(sql);
			ps.setString(1, student.getName());
			ps.setString(2, student.getNumber());
			ps.setString(3, student.getAge());
			ps.setString(4, student.getSex());
			ps.setString(5, oldNumber);
			ps.executeUpdate();
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			try {
				if (ps != null)
					ps.close();
			} catch (SQLException sqle) {
			}
			if (con != null)
				freeConnection(con);
		}
		return student;
	}

	//       
	public void deleteStudentBean(String number) {
		Connection con = null;
		PreparedStatement ps = null;
		String sql = "delete from student where number=?";
		try {
			con = getConnection();
			ps = con.prepareStatement(sql);
			ps.setString(1, number);
			ps.executeUpdate();
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			try {
				if (ps != null)
					ps.close();
			} catch (SQLException sqle) {
			}
			if (con != null)
				freeConnection(con);
		}
	}

	//         
	public ArrayList<StudentBean> selectAllStudent() {
		Connection con = null;
		PreparedStatement ps = null;
		String sql = "select * from student";
		ArrayList<StudentBean> mList = new ArrayList<StudentBean>();
		try {
			con = getConnection();
			ps = con.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				StudentBean student = new StudentBean();

				student.setName(rs.getString("name"));
				student.setNumber(rs.getString("number"));
				student.setAge(rs.getString("age"));
				student.setSex(rs.getString("sex"));

				mList.add(student);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			try {
				if (ps != null)
					ps.close();
			} catch (SQLException sqle) {
			}
			if (con != null)
				freeConnection(con);
		}
		return mList;
	}

	//         
	public StudentBean seleStudentBeans(String number) {
		Connection con = null;
		PreparedStatement ps = null;
		String sql = "select * from student where number =?";
		StudentBean student = new StudentBean();
		try {
			con = getConnection();
			ps = con.prepareStatement(sql);
			ps.setString(1, number);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				student.setName(rs.getString("name"));
				student.setNumber(rs.getString("number"));
				student.setAge(rs.getString("age"));
				student.setSex(rs.getString("sex"));
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			try {
				if (ps != null)
					ps.close();
			} catch (SQLException sqle) {
			}
			if (con != null)
				freeConnection(con);
		}
		return student;
	}
}

데이터 작업 섹션 Imp:
교사imp:
package com.sc.java.managers.imp;

import com.sc.java.managers.bean.TeacherBean;
import com.sc.java.managers.db.TeacherDB;
import com.sc.java.managers.table.StudentTable;

public class TeacherImp {
	private TeacherDB db = TeacherDB.getInstance();
	private String account;
	String passWord = null;
	boolean isTeacher = db.selectTeacherAccount(account);

	//         
	public void registerTeacher() {
		if (!isTeacher) {
			String name = null;
			TeacherBean teacher = new TeacherBean(name, account, passWord);
			db.addTeacher(teacher);
		}
	}

	//         
	public void loginTeacher() {
		boolean isTeacher = db.selectTeacherLogin(account, passWord);
		if (isTeacher) {
			StudentTable stu = new StudentTable();
			stu.init();
		} else {
			loginTeacher();
		}
	}
}

학생imp:
package com.sc.java.managers.imp;

import java.util.ArrayList;
import com.sc.java.managers.bean.StudentBean;
import com.sc.java.managers.db.StudentDB;

public class StudentImp {
	public ArrayList<StudentImp> mStudentList = new ArrayList<StudentImp>();//       
	String number = null;
	String name = null;
	String age = null;
	String sex = null;
	private StudentDB db = StudentDB.getInstance();
	StudentBean student = new StudentBean(name, number, age, sex);
	boolean isStudent = db.selectStudentByNumber(number);

	//     
	public void addStudent(StudentBean student) {
		if (!isStudent) {
			db.addStudent(student);
		} else {
			System.out.println("         ,     !");
		}

	}

	//     
	public void deleteStudent(String number) {
		boolean isStudent = db.selectStudentByNumber(number);
		if (!isStudent) {
			System.out.println("       :" + number + "      !");
		} else {
			db.deleteStudentBean(number);
		}
	}

	//         
	public boolean findStudentByNumber(String number) {
		boolean isStudent = db.selectStudentByNumber(number);
		if (!isStudent) {
			System.out.println("       " + number + "      !");
		} else {
			db.seleStudentBeans(number);
		}
		return isStudent;
	}

	//       
	public void updateStudent(StudentBean student) {
		boolean isStudent = db.selectStudentByNumber(number);

		if (!isStudent) {
			System.out.println("       " + number + "      !");
		} else {
			db.updateStudentBean(student, number);
		}
	}

	//       
	public void showAllStudent() {
		System.out.println("   " + " \t" + "  " + "\t" + "  " + "\t" + "  ");
		ArrayList<StudentBean> list = db.selectAllStudent();
		for (StudentBean students : list) {
			System.out.println(students.getName() + "\t" + students.getNumber()
					+ "\t" + students.getAge() + "\t" + students.getSex());
		}
	}

	//       
	public ArrayList<StudentBean> showAllStudents() {
		ArrayList<StudentBean> list = db.selectAllStudent();
		return list;
	}

	//         
	public void exit() {
		System.out.println("====           ====");
		System.out.println("===             ===");
		System.exit(0);

	}
}

교사가 로그인한 후 조작 인터페이스 창은 다음과 같다.
package com.sc.java.managers.table;

import java.util.Vector;

import javax.swing.table.AbstractTableModel;

public class ConnectStudent extends AbstractTableModel {

	private static final long serialVersionUID = -7495940408592595397L;

	private Vector content = null;

	private String[] titleCols = { "ID", "  ", "  ", "  " };

	public ConnectStudent() {
		content = new Vector();
	}

	public ConnectStudent(int count) {
		content = new Vector(count);
	}

	public void addRow(String name, String sex, int age) {
		Vector vc = new Vector(4);
		vc.add(0, new Integer(content.size()));
		vc.add(1, name);
		vc.add(2, sex);
		vc.add(3, age);
		content.add(vc);
	}

	public void removeRow(int row) {
		content.remove(row);
	}

	public void removeRows(int row, int count) {
		for (int i = 0; i < count; i++) {
			if (content.size() > row) {
				content.remove(row);
			}
		}
	}


	public boolean isCellEditable(int rowIndex, int columnIndex) {
		if (columnIndex == 0) {
			return false;
		}
		return true;
	}

	public String getColumnName(int col) {
		return titleCols[col];
	}

	public int getColumnCount() {
		return titleCols.length;
	}

	public int getRowCount() {
		return content.size();
	}

	public Object getValueAt(int row, int col) {
		return ((Vector) content.get(row)).get(col);
	}

	/**
	 *         
	 */
	public void setValueAt(Object value, int row, int col) {
		((Vector) content.get(row)).remove(col);
		((Vector) content.get(row)).add(col, value);
		this.fireTableCellUpdated(row, col);
	}

	/**
	 *       
	 */
	public Class getColumnClass(int col) {
		return getValueAt(0, col).getClass();
	}
}

 
package com.sc.java.managers.table;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import com.sc.java.managers.bean.StudentBean;
import com.sc.java.managers.imp.StudentImp;

public class StudentTable extends JFrame {

	private StudentImp stu = new StudentImp();
	Container container;
	ConnectStudent model;
	JTable table;
	JLabel stuName, stuNumber, stuAge, stuSex;
	JScrollPane tableJSPane;
	JTextField stuNameTxt, stuNumberTxt, stuAgeTxt, stuSexTxt;
	JButton addBtn, deleteBtn, updateBtn, slectBtn, allBtn, confirmBtn;
	JPanel showPanel, jp1, jp2, jp3, jp4, jp5;

	public StudentTable() {
		super("         ");
		this.setLocation(450, 100);
		this.setSize(600, 400);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public void init() {
		
		addBtn = new JButton("    ");
		deleteBtn = new JButton("    ");
		updateBtn = new JButton("    ");
		slectBtn = new JButton("    ");
		allBtn = new JButton("      ");

		showPanel = new JPanel();
		model = new ConnectStudent(20);
		table = new JTable(model);
		tableJSPane = new JScrollPane(table);

		showPanel.add(addBtn);
		showPanel.add(deleteBtn);
		showPanel.add(updateBtn);
		showPanel.add(slectBtn);
		showPanel.add(allBtn);

		table.setBackground(Color.white);

		this.add(tableJSPane, BorderLayout.CENTER);
		this.add(showPanel, BorderLayout.NORTH);
		
		//       
		
		addBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addStudent();
				table.updateUI();
			}
		});
		//       
		deleteBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				deleteStudent();
				table.updateUI();
			}
		});
		//       
		updateBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				updateStudent();
				table.updateUI();
			}
		});
		//       
		slectBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				selectStuddent();
				table.updateUI();
			}
		});
		//       
		allBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				showAllStudent();
				table.updateUI();
			}
		});

		this.setVisible(true);
	}

	//       
	public void addStudent() {
		JFrame addStu = new JFrame("          ");
		addStu.setBounds(400, 200, 300, 200);
		container = addStu.getContentPane();
		container.setLayout(new GridLayout(5, 1));
		addBtn = new JButton(" 	 ");
		
		stuNumber = new JLabel(" 	 :");
		stuName = new JLabel(" 	 :");
		stuAge = new JLabel(" 	 :");
		stuSex = new JLabel(" 	 :");
		
		stuNumberTxt = new JTextField(10);
		stuNameTxt = new JTextField(10);
		stuAgeTxt = new JTextField(10);
		stuSexTxt = new JTextField(10);
		
		jp1 = new JPanel();
		jp2 = new JPanel();
		jp3 = new JPanel();
		jp4 = new JPanel();
		jp5 = new JPanel();
		
		jp1.add(stuName);
		jp1.add(stuNameTxt);
		jp2.add(stuNumber);
		jp2.add(stuNumberTxt);
		jp3.add(stuAge);
		jp3.add(stuAgeTxt);
		jp4.add(stuSex);
		jp4.add(stuSexTxt);
		jp5.add(addBtn);
		
		container.add(jp1);
		container.add(jp2);
		container.add(jp3);
		container.add(jp4);
		container.add(jp5);
		
		addStu.setVisible(true);
		addBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String name = stuNameTxt.getText();
				String number = stuNumberTxt.getText();
				String age = stuAgeTxt.getText();
				String sex = stuSexTxt.getText();
				if (number.equals("") || name.equals("") || age.equals("")
						|| sex.equals("")) {
					JOptionPane.showMessageDialog(null, "        !");
					return;
				}
				boolean flag = stu.findStudentByNumber(number);
				if (!flag) {
					StudentBean student = new StudentBean(name, number, age,
							sex);
					stu.addStudent(student);
					JOptionPane.showMessageDialog(null, "    !");
					StudentTable.this.setVisible(true);
				} else {
					JOptionPane.showMessageDialog(null, "        !");
				}
			}
		});
	}

	//       
	public void deleteStudent() {
		JFrame deleteStu = new JFrame("          ");
		deleteStu.setBounds(400, 200, 300, 200);
		container = deleteStu.getContentPane();
		container.setLayout(new GridLayout(3, 1));
		confirmBtn = new JButton(" 	 ");
		stuNumber = new JLabel(" 	 :");
		stuNumberTxt = new JTextField(10);
		
		jp1 = new JPanel();
		jp2 = new JPanel();
		
		jp1.add(stuNumber);
		jp1.add(stuNumberTxt);
		jp2.add(confirmBtn);
		
		container.add(jp1);
		container.add(jp2);
		
		deleteStu.setVisible(true);
		confirmBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String number = stuNumberTxt.getText();
				if (number.equals("")) {
					JOptionPane.showMessageDialog(null, "        !");
					return;
				}
				boolean flag = stu.findStudentByNumber(number);
				if (!flag) {
					JOptionPane.showMessageDialog(null, "        !");
				} else {
					stu.deleteStudent(number);
					JOptionPane.showMessageDialog(null, "    !");
					StudentTable.this.setVisible(true);
				}
			}
		});
	}

	//         
	public void selectStuddent() {
		JFrame selectStu = new JFrame("          ");
		selectStu.setBounds(400, 200, 300, 200);
		container = selectStu.getContentPane();
		container.setLayout(new GridLayout(3, 1));
		confirmBtn = new JButton(" 	 ");
		stuNumber = new JLabel(" 	 :");
		stuNumberTxt = new JTextField(10);
		
		jp1 = new JPanel();
		jp2 = new JPanel();
		
		jp1.add(stuNumber);
		jp1.add(stuNumberTxt);
		jp2.add(confirmBtn);
		
		container.add(jp1);
		container.add(jp2);
		
		selectStu.setVisible(true);
		
		confirmBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String number = stuNumberTxt.getText();
				if (number.equals("")) {
					JOptionPane.showMessageDialog(null, "        !");
					return;
				}
				boolean flag = stu.findStudentByNumber(number);
				if (!flag) {
					JOptionPane.showMessageDialog(null, "        !");
				} else {
					stu.findStudentByNumber(number);
					JOptionPane.showMessageDialog(null, "    !");

					StudentTable.this.setVisible(true);
				}

			}
		});
	}

	//          
	public void showAllStudent() {
		JFrame showAllStu = new JFrame("            ");
		showAllStu.setBounds(400, 200, 300, 200);
		container = showAllStu.getContentPane();
		container.setLayout(new GridLayout(3, 1));
		confirmBtn = new JButton(" 	 ");
		
		jp1 = new JPanel();
		jp1.add(confirmBtn);
		container.add(jp1);
		showAllStu.setVisible(true);
		
		confirmBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//                table  
				ArrayList<StudentBean> lists = stu.showAllStudents();
				System.out.println("list >>> :" + lists.size());
				for (StudentBean student : lists) {
					System.out.println("Name >>> " + student.getName());
					model.addRow(student.getName(), student.getSex(), Integer
							.valueOf(student.getAge()));

				}
				table.updateUI();
				// stu.showAllStudent();
			}
		});
	}

	//       
	public void updateStudent() {
		JFrame updateStu = new JFrame("          ");
		updateStu.setBounds(400, 200, 300, 200);
		container = updateStu.getContentPane();
		container.setLayout(new GridLayout(3, 1));
		confirmBtn = new JButton(" 	 ");
		jp1 = new JPanel();
		jp1.add(confirmBtn);
		container.add(jp1);
		updateStu.setVisible(true);
		confirmBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JFrame studentMassge = new JFrame("        ");
				studentMassge.setBounds(400, 200, 300, 200);
				container = studentMassge.getContentPane();
				container.setLayout(new GridLayout(5, 1));

				confirmBtn = new JButton(" 	 ");

				stuNumber = new JLabel(" 	 :");
				stuName = new JLabel(" 	 :");
				stuAge = new JLabel(" 	 :");
				stuSex = new JLabel(" 	 :");

				stuNumberTxt = new JTextField(10);
				stuNameTxt = new JTextField(10);
				stuAgeTxt = new JTextField(10);
				stuSexTxt = new JTextField(10);

				jp1 = new JPanel();
				jp2 = new JPanel();
				jp3 = new JPanel();
				jp4 = new JPanel();
				jp5 = new JPanel();

				jp1.add(stuName);
				jp1.add(stuNameTxt);
				jp2.add(stuNumber);
				jp2.add(stuNumberTxt);
				jp3.add(stuAge);
				jp3.add(stuAgeTxt);
				jp4.add(stuSex);
				jp4.add(stuSexTxt);
				jp5.add(confirmBtn);

				container.add(jp1);
				container.add(jp2);
				container.add(jp3);
				container.add(jp4);
				container.add(jp5);
				//       
				confirmBtn.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						String name = stuNameTxt.getText();
						String number = stuNumberTxt.getText();
						String age = stuAgeTxt.getText();
						String sex = stuSexTxt.getText();
						if (number.equals("") || name.equals("")
								|| age.equals("") || sex.equals("")) {
							JOptionPane.showMessageDialog(null, "        !");
							return;
						}
						boolean flag = stu.findStudentByNumber(number);
						if (!flag) {
							StudentBean student = new StudentBean(name, number,
									age, sex);
							stu.addStudent(student);
							JOptionPane.showMessageDialog(null, "    !");
							StudentTable.this.setVisible(true);
						} else {
							JOptionPane.showMessageDialog(null, "         !");
						}
					}
				});
			}
		});
	}

	//          
	public static void main(String args[]) {
		StudentTable s = new StudentTable();
		s.init();
		// s.addStudent();
		// s.deleteStudent();
		// s.selectStuddent();
		// s.updateStudent();
	}
}

좋은 웹페이지 즐겨찾기