⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 operationui.java

📁 ibatis + sqlserver 学生成绩管理
💻 JAVA
字号:
package ui;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import bean.Student;
import dao.IStudentDAO;
import dao.IStudentDAOImpl;

public class OperationUI extends JFrame implements ActionListener {
	private static final long serialVersionUID = 9207977150918136103L;
	JLabel numLab, nameLab, sexLab, ageLab, commentLab, messageLab;
	JTextField numTxt, nameTxt, sexTxt, ageTxt, commentTxt, resultTxt;
	JButton queryAllBut, queryBut, addBut, updateBut, delBut;
	JPanel northPan, centerPan, southPan;

	OperationUI() {
		queryAllBut = new JButton("全部查询");
		queryBut = new JButton("单个查询");
		addBut = new JButton("添加");
		updateBut = new JButton("更新");
		delBut = new JButton("删除");
		northPan = new JPanel();
		northPan.add(queryAllBut);
		northPan.add(queryBut);
		northPan.add(addBut);
		northPan.add(updateBut);
		northPan.add(delBut);

		numLab = new JLabel("学号:", JLabel.CENTER);
		nameLab = new JLabel("姓名:", JLabel.CENTER);
		sexLab = new JLabel("性别:", JLabel.CENTER);
		ageLab = new JLabel("年龄:", JLabel.CENTER);
		commentLab = new JLabel("备注:", JLabel.CENTER);
		numTxt = new JTextField(1);
		nameTxt = new JTextField(15);
		sexTxt = new JTextField(15);
		ageTxt = new JTextField(15);
		commentTxt = new JTextField(20);

		centerPan = new JPanel();
		centerPan.setLayout(new GridLayout(3, 4, 0, 5));
		centerPan.add(numLab);
		centerPan.add(numTxt);
		centerPan.add(nameLab);
		centerPan.add(nameTxt);
		centerPan.add(sexLab);
		centerPan.add(sexTxt);
		centerPan.add(ageLab);
		centerPan.add(ageTxt);
		centerPan.add(commentLab);
		centerPan.add(commentTxt);

		messageLab = new JLabel("结果");
		resultTxt = new JTextField(30);
		resultTxt.setEditable(false);
		southPan = new JPanel();
		southPan.setSize(40, 30);
		southPan.add(messageLab);
		southPan.add(resultTxt);

		Container container = getContentPane();
		container.add("Center", centerPan);
		container.add("North", northPan);
		container.add("South", southPan);

		queryAllBut.addActionListener(this);
		queryBut.addActionListener(this);
		addBut.addActionListener(this);
		updateBut.addActionListener(this);
		delBut.addActionListener(this);

		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		this.setSize(390, 230);
		this.setResizable(false);
		Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize();
		this.setLocation((scrSize.width - this.getSize().width) / 2,
				(scrSize.height - this.getSize().height) / 2);
		setVisible(true);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == queryBut) {
			queryStudent();
		} else if (e.getSource() == addBut) {
			add();
		} else if (e.getSource() == updateBut) {
			update();
		} else if (e.getSource() == queryAllBut) {
			queryAll();
		} else if (e.getSource() == delBut) {
			delete();
		}
	}

	public boolean checkData() {
		int errorCount = 0;
		if (numTxt.getText().trim().length() != 10) {
			numTxt.setText("长度为10!");
			errorCount++;
		}

		if (nameTxt.getText().trim().length() == 0) {
			nameTxt.setText("不能为空!");
			errorCount++;
		}

		if (!sexTxt.getText().trim().equals("男")
				&& !sexTxt.getText().trim().equals("女")) {
			sexTxt.setText("数据有误!");
			errorCount++;
		}

		try {
			Integer.valueOf(ageTxt.getText());
		} catch (NumberFormatException e) {
			ageTxt.setText("数据有误!!");
			errorCount++;
		}
		return errorCount == 0;
	}

	public void queryStudent() {
		IStudentDAO studentDAO = new IStudentDAOImpl();
		Student student = studentDAO.queryAllStudentById(numTxt.getText()
				.trim());
		if (student == null) {
			resultTxt.setText("查无此人,请查证后在试!");
			resetData();
			return;
		}
		nameTxt.setText(student.getSname());
		sexTxt.setText(student.getSex());
		ageTxt.setText(String.valueOf(student.getAge()));
		commentTxt.setText(student.getComment());
		resultTxt.setText("查询成功!");
	}

	private void resetData() {
		numTxt.setText("");
		nameTxt.setText("");
		sexTxt.setText("");
		ageTxt.setText("");
		commentTxt.setText("");
	}

	public void add() {
		if (checkData() == false) {
			resultTxt.setText("数据有误,操作失败!");
			return;
		}

		IStudentDAO studentDAO = new IStudentDAOImpl();
		Student validate = studentDAO.queryAllStudentById(numTxt.getText());
		if (validate != null) {
			resultTxt.setText("该用学生已存在,请使用更新操作!");
			return;
		}

		Student student = gatherInfo();
		studentDAO.addStudent(student);
		resultTxt.setText("添加成功!");
	}

	private Student gatherInfo() {
		Student student = new Student();
		student.setSid(numTxt.getText());
		student.setSname(nameTxt.getText());
		student.setSex(sexTxt.getText());
		student.setAge(Integer.valueOf(ageTxt.getText()));
		student.setComment(commentTxt.getText());
		return student;
	}

	public void update() {
		if (checkData() == false) {
			resultTxt.setText("数据有误,操作失败!");
			return;
		}

		IStudentDAO studentDAO = new IStudentDAOImpl();
		Student student = gatherInfo();
		studentDAO.updateStudentById(student);
		resultTxt.setText("更新成功!");
	}

	public void delete() {
		IStudentDAO studentDAO = new IStudentDAOImpl();
		studentDAO.deleteStudentById(numTxt.getText());
		resetData();
		resultTxt.setText("删除成功!");
	}

	public void queryAll() {
		new ShowStudentUI();
	}

	public static void main(String[] s) {
		new OperationUI();
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -